while loop in Bash Scripting - Time & Space Complexity
We want to understand how the time a while loop takes changes as the input size grows.
Specifically, how many times does the loop run when input gets bigger?
Analyze the time complexity of the following code snippet.
count=0
limit=100
while [ $count -lt $limit ]
do
echo "Count is $count"
((count++))
done
This code prints numbers from 0 up to one less than the limit using a while loop.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop runs the echo and increment commands repeatedly.
- How many times: It runs once for each number from 0 up to limit minus one, so limit times.
As the limit grows, the number of loop runs grows the same way.
| Input Size (limit) | Approx. Operations (loop runs) |
|---|---|
| 10 | 10 |
| 100 | 100 |
| 1000 | 1000 |
Pattern observation: The number of operations grows directly with the input size.
Time Complexity: O(n)
This means the time taken grows in a straight line as the input size increases.
[X] Wrong: "The while loop runs a fixed number of times no matter the input."
[OK] Correct: The loop count depends on the limit variable, so if limit grows, the loop runs more times.
Understanding how loops grow with input helps you explain script performance clearly and confidently.
"What if we changed the increment from 1 to 2 each time? How would the time complexity change?"
Practice
while loop do in a bash script?Solution
Step 1: Understand the purpose of a while loop
A while loop runs repeatedly while its condition remains true.Step 2: Compare options with the definition
Only Repeats commands as long as a condition is true correctly describes this behavior.Final Answer:
Repeats commands as long as a condition is true -> Option DQuick Check:
while loop = repeat while true [OK]
- Thinking while loops run only once
- Confusing while with if statements
- Assuming it runs when condition is false
Solution
Step 1: Recall bash while loop syntax
The correct syntax uses square brackets for condition and 'do' keyword.Step 2: Match options with correct syntax
while [ condition ]; do matches the correct bash syntax:while [ condition ]; do.Final Answer:
while [ condition ]; do -> Option AQuick Check:
Correct bash while syntax = while [ condition ]; do [OK]
- Using parentheses instead of square brackets
- Missing 'do' keyword
- Using curly braces incorrectly
count=1 while [ $count -le 3 ]; do echo $count count=$((count + 1)) done
Solution
Step 1: Analyze the loop condition and increments
The loop starts with count=1 and runs while count is less or equal to 3, incrementing count by 1 each time.Step 2: Trace the output for each iteration
It prints 1, then 2, then 3, then stops because count becomes 4.Final Answer:
1 2 3 -> Option BQuick Check:
Loop prints 1 to 3 [OK]
- Forgetting to increment count causing infinite loop
- Expecting output beyond 3
- Confusing loop condition operators
count=1 while [ $count -lt 5 ] echo $count count=$((count + 1)) done
Solution
Step 1: Check while loop syntax
The while loop must have 'do' after the condition to start the loop body.Step 2: Identify missing 'do'
The script misses 'do' after the condition line, causing syntax error.Final Answer:
Missing 'do' keyword after while condition -> Option CQuick Check:
while loop needs 'do' [OK]
- Omitting 'do' keyword
- Misplacing brackets
- Incorrect increment syntax
Solution
Step 1: Understand the goal
We want to print even numbers from 2 to 10 inclusive.Step 2: Check each option's logic
num=2 while [ $num -le 10 ]; do echo $num num=$((num + 1)) done starts at 2 and increments by 1 up to 10, printing 2 3 4 5 6 7 8 9 10 - incorrect.
num=1 while [ $num -le 10 ]; do if [ $((num % 2)) -eq 0 ]; then echo $num; fi num=$((num + 1)) done starts at 1, increments by 1, and prints only if even - correct.
num=2 while [ $num -lt 10 ]; do echo $num num=$((num + 2)) done stops before 10, missing 10.
num=0 while [ $num -le 10 ]; do echo $num num=$((num + 2)) done starts at 0, which is even but outside requested range (2 to 10).Step 3: Choose the best option
Only the script using modulo to explicitly check evenness prints exactly 2,4,6,8,10.Final Answer:
num=1 while [ $num -le 10 ]; do if [ $((num % 2)) -eq 0 ]; then echo $num; fi num=$((num + 1)) done -> Option AQuick Check:
Check even with modulo inside loop [OK]
- Stopping loop before 10
- Starting from 0 when 2 is required
- Not checking evenness explicitly
Ready to prove it?
Pass the test and this pattern moves unknown
