C-style for loop in Bash Scripting - Time & Space Complexity
When we use a C-style for loop in bash, it is important to know how the time it takes to run changes as we increase the number of loop steps.
We want to find out how the total work grows when the loop runs more times.
Analyze the time complexity of the following code snippet.
for (( i=0; i<N; i++ )); do
echo "Step $i"
done
This code runs a loop from 0 up to N-1, printing a message each time.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The loop runs the echo command once per iteration.
- How many times: Exactly N times, where N is the input size.
As N grows, the number of times the loop runs grows the same way.
| Input Size (N) | Approx. Operations |
|---|---|
| 10 | 10 echo commands |
| 100 | 100 echo commands |
| 1000 | 1000 echo commands |
Pattern observation: The work grows directly in proportion to N. Double N, double the work.
Time Complexity: O(N)
This means the time to finish grows linearly as the number of loop steps increases.
[X] Wrong: "The loop runs in constant time because it just prints one line."
[OK] Correct: Even though each step is simple, the loop runs N times, so total time grows with N, not fixed.
Understanding how loops affect time helps you explain your code clearly and shows you can think about efficiency, a skill valued in many scripting tasks.
"What if we added a nested C-style for loop inside this loop? How would the time complexity change?"
Practice
for ((i=1; i<=3; i++)); do echo $i; doneSolution
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.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.Final Answer:
Prints numbers 1, 2, and 3 each on a new line -> Option AQuick Check:
Loop from 1 to 3 prints 1, 2, 3 [OK]
- Starting loop at 0 instead of 1
- Confusing <= with < in condition
- Forgetting to increment i
Solution
Step 1: Identify correct C-style for loop syntax in bash
Bash requires double parentheses (( )) around the loop control with semicolons separating init, condition, and increment.Step 2: Check each option
for ((i=0; i<5; i++)); do echo $i; done uses double parentheses and semicolons correctly; others use incorrect brackets or missing parentheses.Final Answer:
for ((i=0; i<5; i++)); do echo $i; done -> Option BQuick Check:
Double parentheses and semicolons = correct syntax [OK]
- Using single parentheses instead of double
- Using square or curly brackets
- Missing semicolons between parts
for ((i=2; i<=6; i+=2)); do echo $i; done
Solution
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.Step 2: List values printed
Values printed are 2, then 4, then 6. After 6, i becomes 8 which breaks the condition.Final Answer:
2 4 6 -> Option DQuick Check:
Increment by 2 from 2 to 6 prints 2,4,6 [OK]
- Assuming increment is 1 by default
- Including numbers beyond the condition
- Confusing <= with <
for ((i=0; i<5 i++)); do echo $i; done
Solution
Step 1: Check loop syntax parts
The loop control must have three parts separated by semicolons: initialization, condition, and increment.Step 2: Locate missing semicolon
Between 'i<5' and 'i++' there is no semicolon, causing a syntax error.Final Answer:
Missing semicolon between condition and increment -> Option AQuick Check:
Semicolons separate parts in C-style for loops [OK]
- Omitting semicolons between parts
- Using single parentheses
- Forgetting do keyword
Solution
Step 1: Analyze for ((i=1; i<=9; i+=2)); do echo $i; done
for ((i=1; i<=9; i+=2)); do echo $i; done starts at 1, increments by 2, prints 1,3,5,7,9 directly.Step 2: Analyze options B and C
Both loop with i++ and check if (( i % 2 == 0 )), which selects even numbers: B prints 0,2,4,6,8; C prints 2,4,6,8.Final Answer:
for ((i=1; i<=9; i+=2)); do echo $i; done -> Option CQuick Check:
Increment by 2 from 1 to <=9 prints odds 1-9 [OK]
- Using wrong increment step
- Starting from 0 without filtering
- Forgetting to check odd condition
Ready to prove it?
Pass the test and this pattern moves unknown
