Bird
0
0

What does the following C-style for loop do in bash?

easy🧠 Conceptual Q11 of 15
Bash Scripting - Loops
What does the following C-style for loop do in bash?
for ((i=1; i<=3; i++)); do echo $i; done
APrints numbers 1, 2, and 3 each on a new line
BPrints numbers 0, 1, and 2 each on a new line
CPrints numbers 1, 2, 3, and 4 each on a new line
DCauses a syntax error
Step-by-Step Solution
Solution:
  1. Step 1: Understand the loop initialization and condition

    The loop starts with i=1 and runs while i is less than or equal to 3.
  2. Step 2: Analyze the increment and output

    Each iteration prints the current value of i, then increases i by 1 until i becomes 4 and stops.
  3. Final Answer:

    Prints numbers 1, 2, and 3 each on a new line -> Option A
  4. Quick Check:

    Loop from 1 to 3 prints 1, 2, 3 [OK]
Quick Trick: Check start, end, and increment values carefully [OK]
Common Mistakes:
MISTAKES
  • Starting loop at 0 instead of 1
  • Confusing <= with < in condition
  • Forgetting to increment i

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes