Infinite loops in Bash Scripting - Time & Space Complexity
Infinite loops run without stopping, so their execution time keeps growing endlessly.
We want to understand how this affects the time complexity of a script.
Analyze the time complexity of the following code snippet.
while true; do
echo "Running..."
sleep 1
done
This script prints a message every second forever, never stopping.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The infinite while loop that repeats printing and sleeping.
- How many times: It runs endlessly without stopping.
Since the loop never ends, the number of operations keeps increasing without limit.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 seconds | About 10 prints and sleeps |
| 100 seconds | About 100 prints and sleeps |
| 1000 seconds | About 1000 prints and sleeps |
Pattern observation: The operations grow linearly with time, but since it never stops, it grows without bound.
Time Complexity: O(\infty)
This means the script runs forever, so the time it takes keeps increasing endlessly.
[X] Wrong: "Infinite loops have a fixed time complexity like other loops."
[OK] Correct: Infinite loops never stop, so their execution time is not fixed or bounded like normal loops.
Understanding infinite loops helps you recognize when a script might run forever, which is important for writing reliable automation.
"What if we add a condition inside the loop to stop after 100 iterations? How would the time complexity change?"
Practice
Solution
Step 1: Understand infinite loop syntax
In bash,while trueruns the loop forever because the condition is always true.Step 2: Check each option
while true; do echo Hello; doneuseswhile truecorrectly.for i in 1..10; do echo $i; doneloops 10 times, not infinite.if true; then echo Loop; fiis a conditional, not a loop.while false; do echo No; donenever runs because condition is false.Final Answer:
<code>while true; do echo Hello; done</code> -> Option DQuick Check:
Infinite loop = while true [OK]
- Confusing finite loops with infinite loops
- Using false as loop condition
- Using if instead of while
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; donematches infinite loop syntax.for i in {1..5}; do echo $i; doneloops 5 times.for i in ; do echo $i; doneis invalid syntax.for (( i=0; i<5; i++ )); do echo $i; doneloops 5 times with increment.Final Answer:
<code>for (( ; ; )); do echo Loop; done</code> -> Option CQuick Check:
Infinite for loop = for (( ; ; )) [OK]
- Leaving out semicolons in for loop syntax
- Using range limits in infinite loops
- Trying to use 'for i in' without values
count=1
while :; do
echo $count
((count++))
if [ $count -gt 3 ]; then
break
fi
doneSolution
Step 1: Understand the loop condition
The loop useswhile :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 AQuick Check:
Break stops infinite loop at count 4 [OK]
- Assuming infinite loop prints forever
- Counting one extra iteration
- Confusing ':' with 'true'
while true echo "Running" done
Solution
Step 1: Check while loop syntax
Bash requires the keyworddoafter the while condition to start the loop body.Step 2: Analyze the script
The script haswhile truebut nodobeforeecho. This causes a syntax error.Final Answer:
Missing 'do' keyword after while condition -> Option BQuick Check:
while loops need 'do' before commands [OK]
- Forgetting 'do' after while condition
- Misplacing semicolons
- Confusing 'done' with 'do'
stop.txt exists. Which script correctly implements this?Solution
Step 1: Check loop structure and break condition
while true; do if [ -f stop.txt ]; then break fi echo "Waiting..." sleep 2 doneuseswhile truewith a break ifstop.txtexists, which stops the loop correctly.Step 2: Verify order and commands
It checks forstop.txtfirst 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 AQuick Check:
Break on file check inside infinite loop [OK]
- Placing break after sleep without file check
- Using exit instead of break inside loop
- Incorrect loop syntax or missing do/done
Ready to prove it?
Pass the test and this pattern moves unknown
