📖 Scenario: You are learning how to create loops in Bash scripts. Loops help you repeat tasks automatically, like checking if a file exists or waiting for user input. Sometimes, you want a loop to run forever until you stop it manually. This is called an infinite loop.Imagine you want to create a simple script that keeps printing a message every second until you stop it. This is useful for monitoring or waiting for something to happen.
🎯 Goal: Build a Bash script that uses an infinite loop to print the message "Hello, world!" every second.
📋 What You'll Learn
Create an infinite loop using the correct Bash syntax
Use the echo command to print the message
Use the sleep command to pause for 1 second between prints
Print the message repeatedly until the script is stopped manually
💡 Why This Matters
🌍 Real World
Infinite loops are used in scripts that monitor system status, wait for events, or run servers that need to stay active until stopped.
💼 Career
Understanding infinite loops is important for automation, system administration, and writing scripts that handle continuous tasks or services.
Progress0 / 4 steps
1
Create an infinite loop using while true
Write a Bash script that starts an infinite loop using while true; do and ends it with done. This loop will run forever.
Bash Scripting
Hint
Use while true; do to start the loop and done to end it. The colon : is a placeholder command that does nothing.
2
Add a message to print inside the loop
Inside the infinite loop, add the command echo "Hello, world!" to print the message every time the loop runs.
Bash Scripting
Hint
Use echo "Hello, world!" inside the loop to print the message.
3
Pause the loop for 1 second between prints
Add the command sleep 1 inside the loop after the echo command to pause the script for 1 second between each message.
Bash Scripting
Hint
Use sleep 1 to pause the loop for one second.
4
Run the script and observe the output
Run the complete script. It should print Hello, world! every second repeatedly. Use Ctrl+C to stop the script when you want.
Bash Scripting
Hint
Run the script in your terminal. You will see the message printed every second until you stop it.
Practice
(1/5)
1. Which of the following is a correct way to start an infinite loop in bash?
easy
A. if true; then echo Loop; fi
B. for i in 1..10; do echo $i; done
C. while false; do echo No; done
D. while true; do echo Hello; done
Solution
Step 1: Understand infinite loop syntax
In bash, while true runs the loop forever because the condition is always true.
Step 2: Check each option
while true; do echo Hello; done uses while true correctly. for i in 1..10; do echo $i; done loops 10 times, not infinite. if true; then echo Loop; fi is a conditional, not a loop. while false; do echo No; done never runs because condition is false.
Final Answer:
<code>while true; do echo Hello; done</code> -> Option D
Quick Check:
Infinite loop = while true [OK]
Hint: Infinite loops use always-true conditions like 'while true' [OK]
Common Mistakes:
Confusing finite loops with infinite loops
Using false as loop condition
Using if instead of while
2. Which of the following is the correct syntax for an infinite for loop in bash?
easy
A. for (( i=0; i<5; i++ )); do echo $i; done
B. for i in {1..5}; do echo $i; done
C. for (( ; ; )); do echo Loop; done
D. for i in ; do echo $i; done
Solution
Step 1: Recall infinite for loop syntax
In bash, for (( ; ; )) creates an infinite loop because it has no start, condition, or increment limits.
Step 2: Analyze options
for (( ; ; )); do echo Loop; done matches infinite loop syntax. for i in {1..5}; do echo $i; done loops 5 times. for i in ; do echo $i; done is invalid syntax. for (( i=0; i<5; i++ )); do echo $i; done loops 5 times with increment.
Final Answer:
<code>for (( ; ; )); do echo Loop; done</code> -> Option C
Quick Check:
Infinite for loop = for (( ; ; )) [OK]
Hint: Infinite for loops omit all three parts in (( ; ; )) [OK]
Common Mistakes:
Leaving out semicolons in for loop syntax
Using range limits in infinite loops
Trying to use 'for i in' without values
3. What will be the output of this script?
count=1
while :; do
echo $count
((count++))
if [ $count -gt 3 ]; then
break
fi
done
medium
A. 1 2 3
B. 1 2 3 4
C. 1 2 3 4 5
D. Infinite loop printing numbers
Solution
Step 1: Understand the loop condition
The loop uses while : which is an infinite loop, but it has a break condition inside.
Step 2: Trace the loop execution
It prints count starting at 1, increments count, and breaks when count > 3. So it prints 1, 2, 3 and stops before printing 4.
Final Answer:
1 2 3 -> Option A
Quick Check:
Break stops infinite loop at count 4 [OK]
Hint: Break exits infinite loops early [OK]
Common Mistakes:
Assuming infinite loop prints forever
Counting one extra iteration
Confusing ':' with 'true'
4. Identify the error in this infinite loop script:
while true
echo "Running"
done
medium
A. Missing semicolon or newline after 'true'
B. Missing 'do' keyword after while condition
C. Incorrect use of quotes around echo argument
D. Missing 'done' keyword at the end
Solution
Step 1: Check while loop syntax
Bash requires the keyword do after the while condition to start the loop body.
Step 2: Analyze the script
The script has while true but no do before echo. This causes a syntax error.
Final Answer:
Missing 'do' keyword after while condition -> Option B
Quick Check:
while loops need 'do' before commands [OK]
Hint: Always put 'do' after while condition [OK]
Common Mistakes:
Forgetting 'do' after while condition
Misplacing semicolons
Confusing 'done' with 'do'
5. You want to create an infinite loop that prints "Waiting..." every 2 seconds but stops if a file named stop.txt exists. Which script correctly implements this?
hard
A.
while true; do
if [ -f stop.txt ]; then
break
fi
echo "Waiting..."
sleep 2
done
B.
while :
echo "Waiting..."
sleep 2
if [ -f stop.txt ]; then
break
fi
done
C.
for (( ; ; )); do
echo "Waiting..."
sleep 2
if test -f stop.txt; then
break
fi
done
D.
while true; do
echo "Waiting..."
sleep 2
if [ -e stop.txt ]; then
exit
fi
done
Solution
Step 1: Check loop structure and break condition
while true; do
if [ -f stop.txt ]; then
break
fi
echo "Waiting..."
sleep 2
done
uses while true with a break if stop.txt exists, which stops the loop correctly.
Step 2: Verify order and commands
It checks for stop.txt first and breaks if the file exists, otherwise prints "Waiting..." and sleeps 2 seconds. This matches the requirement.
Final Answer:
while true; do
if [ -f stop.txt ]; then
break
fi
echo "Waiting..."
sleep 2
done -> Option A
Quick Check:
Break on file check inside infinite loop [OK]
Hint: Check file then break inside infinite loop [OK]
Common Mistakes:
Placing break after sleep without file check
Using exit instead of break inside loop
Incorrect loop syntax or missing do/done
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.