Bird
0
0

What will be the output of this bash script?

medium📝 Command Output Q13 of 15
Bash Scripting - Loops
What will be the output of this bash script?
for i in 1 2 3 4 5; do
  if [ "$i" -eq 3 ]; then
    break
  fi
  echo $i
done
A1 2 3 4 5
B3 4 5
C1 2 3
D1 2
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the loop and break condition

    The loop runs from 1 to 5. When i equals 3, the break command stops the loop immediately.
  2. Step 2: Determine which values are printed

    Before reaching 3, the script echoes 1 and 2. When i is 3, loop breaks, so 3 is not printed.
  3. Final Answer:

    1 2 -> Option D
  4. Quick Check:

    Break stops at 3, prints before = 1 2 [OK]
Quick Trick: Break stops loop before printing current value [OK]
Common Mistakes:
MISTAKES
  • Including the break value in output
  • Confusing break with continue
  • Assuming loop runs fully

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes