Bird
Raised Fist0
Bash Scriptingscripting~20 mins

Infinite loops in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Infinite Loop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this infinite loop script?
Consider this Bash script snippet. What will it output when run?
Bash Scripting
count=1
while true; do
  echo $count
  ((count++))
  if [ $count -gt 3 ]; then
    break
  fi
done
A
1
2
3
4
B
1
2
3
C
1
2
3
4
5
DNo output, infinite loop
Attempts:
2 left
💡 Hint
Look at the break condition inside the loop.
📝 Syntax
intermediate
2:00remaining
Which option causes a syntax error in this infinite loop?
Identify which Bash loop snippet will cause a syntax error.
Awhile :; do echo "Hello"; done
Bwhile (true) do echo "Hello"; done
Cwhile true; do echo "Hello"; done
Denod ;"olleH" ohce od ;eurt elihw
Attempts:
2 left
💡 Hint
Check the syntax of the while loop condition and keywords.
🔧 Debug
advanced
2:00remaining
Why does this infinite loop never stop?
Examine the script below. Why does it run forever without stopping?
Bash Scripting
i=0
while [ $i -lt 5 ]
do
  echo $i
done
AThe loop has no done keyword.
BThe condition syntax is wrong, causing infinite loop.
CThe echo command causes the loop to restart.
DVariable i is never incremented, so condition is always true.
Attempts:
2 left
💡 Hint
Check if the loop changes the variable controlling the condition.
🚀 Application
advanced
2:00remaining
Which script safely runs an infinite loop that can be stopped by Ctrl+C?
Select the Bash script that runs an infinite loop printing 'Running' every second and can be stopped by pressing Ctrl+C.
Awhile :; do echo "Running"; done
Bfor ((;;)); do echo "Running"; sleep 1; done
Cwhile true; do echo "Running"; sleep 1; done
Dwhile true; do echo "Running"; sleep 0; done
Attempts:
2 left
💡 Hint
Consider which loop includes a pause and allows interruption.
🧠 Conceptual
expert
2:00remaining
What is the main risk of an infinite loop without a break or exit condition in automation scripts?
Why should infinite loops without exit conditions be avoided in automation scripts?
AThey can consume system resources indefinitely, causing performance issues.
BThey always produce syntax errors and fail to run.
CThey automatically stop after 10 minutes to prevent issues.
DThey improve script speed by running continuously without pauses.
Attempts:
2 left
💡 Hint
Think about what happens if a script never stops running.

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

  1. Step 1: Understand infinite loop syntax

    In bash, while true runs the loop forever because the condition is always true.
  2. 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.
  3. Final Answer:

    <code>while true; do echo Hello; done</code> -> Option D
  4. 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

  1. Step 1: Recall infinite for loop syntax

    In bash, for (( ; ; )) creates an infinite loop because it has no start, condition, or increment limits.
  2. 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.
  3. Final Answer:

    <code>for (( ; ; )); do echo Loop; done</code> -> Option C
  4. 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

  1. Step 1: Understand the loop condition

    The loop uses while : which is an infinite loop, but it has a break condition inside.
  2. 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.
  3. Final Answer:

    1 2 3 -> Option A
  4. 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

  1. Step 1: Check while loop syntax

    Bash requires the keyword do after the while condition to start the loop body.
  2. Step 2: Analyze the script

    The script has while true but no do before echo. This causes a syntax error.
  3. Final Answer:

    Missing 'do' keyword after while condition -> Option B
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    while true; do if [ -f stop.txt ]; then break fi echo "Waiting..." sleep 2 done -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard