Bird
0
0

What will be the output of this script?

medium📝 Command Output Q4 of 15
Bash Scripting - Loops
What will be the output of this script?
i=0
while [ $i -lt 3 ]; do
  echo $i
  i=$((i + 1))
done
A0 1 2 3
B1 2 3
C0 1 2
DNo output, infinite loop
Step-by-Step Solution
Solution:
  1. Step 1: Initialize i=0

    The variable i starts at 0.
  2. Step 2: Loop condition [ $i -lt 3 ]

    The loop runs while i is less than 3.
  3. Step 3: Print and increment

    Each iteration prints i and increments it by 1.
  4. Final Answer:

    0 1 2 -> Option C
  5. Quick Check:

    Loop stops before i reaches 3 [OK]
Quick Trick: Loop runs while condition true; stops before i=3 [OK]
Common Mistakes:
MISTAKES
  • Assuming loop prints 3
  • Thinking loop is infinite
  • Confusing increment placement

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes