Bird
0
0

What will be the output of this script?

medium📝 Command Output Q13 of 15
Bash Scripting - Loops
What will be the output of this script?
count=1
while :; do
  echo $count
  ((count++))
  if [ $count -gt 3 ]; then
    break
  fi
done
A1 2 3
B1 2 3 4
C1 2 3 4 5
DInfinite loop printing numbers
Step-by-Step Solution
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]
Quick Trick: Break exits infinite loops early [OK]
Common Mistakes:
MISTAKES
  • Assuming infinite loop prints forever
  • Counting one extra iteration
  • Confusing ':' with 'true'

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes