Bird
0
0

Why does the following C-style for loop in bash print nothing?

hard🧠 Conceptual Q10 of 15
Bash Scripting - Loops
Why does the following C-style for loop in bash print nothing?
for ((i=5; i<5; i++)); do echo $i; done
ABecause increment operator is incorrect
BBecause the loop condition i<5 is false at start, so loop never runs
CBecause echo command is missing
DBecause variable i is not initialized
Step-by-Step Solution
Solution:
  1. Step 1: Check loop initialization and condition

    Loop starts with i=5, condition is i<5, which is false immediately.
  2. Step 2: Understand loop execution

    Since condition is false at start, loop body never executes, so nothing prints.
  3. Final Answer:

    Because the loop condition i<5 is false at start, so loop never runs -> Option B
  4. Quick Check:

    Loop runs only if condition true initially [OK]
Quick Trick: Loop runs only if condition true at start [OK]
Common Mistakes:
MISTAKES
  • Assuming loop runs at least once
  • Confusing increment with condition
  • Ignoring initial condition check

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes