Bird
0
0

What is the output of this bash script?

medium📝 Command Output Q13 of 15
Bash Scripting - Loops
What is the output of this bash script?
for ((i=2; i<=6; i+=2)); do echo $i; done
A2 3 4 5 6
BSyntax error
C2 4 6 8
D2 4 6
Step-by-Step Solution
Solution:
  1. Step 1: Understand loop initialization and increment

    The loop starts at i=2, runs while i is less than or equal to 6, and increments i by 2 each time.
  2. Step 2: List values printed

    Values printed are 2, then 4, then 6. After 6, i becomes 8 which breaks the condition.
  3. Final Answer:

    2 4 6 -> Option D
  4. Quick Check:

    Increment by 2 from 2 to 6 prints 2,4,6 [OK]
Quick Trick: Check increment step and loop condition carefully [OK]
Common Mistakes:
MISTAKES
  • Assuming increment is 1 by default
  • Including numbers beyond the condition
  • Confusing <= with <

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes