while and until Loops
Use a for loop when the collection is known. Use while or until when repetition depends on state that changes over time: service readiness, retry success, input availability, or a counter reaching a threshold.
Learning objectives
By the end of this lesson
- Explain the success-status semantics of while and until.
- Build bounded polling loops with delays.
- Implement explicit retry limits.
- Use loop control without creating infinite automation.
- Choose the correct loop based on the termination condition.
1. while repeats while its condition succeeds
count=1
while (( count <= 3 )); do
printf 'count=%d\n' "$count"
((count += 1))
done
flowchart TD
C{"condition succeeds?"} -->|"yes"| B["run body"]
B --> C
C -->|"no"| E["continue after loop"]
2. until repeats until its condition succeeds
attempt=1
until (( attempt > 3 )); do
printf 'attempt=%d\n' "$attempt"
((attempt += 1))
done
until service_ready can read naturally when the desired
end state is positive.
3. Polling needs a delay
while ! service_ready; do
printf 'not ready\n' >&2
sleep 2
done
Without a delay, a polling loop can waste CPU and overload the dependency being checked.
4. Polling also needs a maximum wait
max_attempts=5
attempt=1
while (( attempt <= max_attempts )); do
if service_ready; then
printf 'ready on attempt %d\n' "$attempt"
break
fi
printf 'attempt %d/%d failed\n' "$attempt" "$max_attempts" >&2
((attempt += 1))
sleep 2
done
if (( attempt > max_attempts )); then
printf 'timed out\n' >&2
exit 1
fi
Every operational wait should define successful termination, failure termination, and a maximum duration.
5. Retry only failures that are appropriate to retry
Temporary network failures may justify retries. Invalid credentials, malformed input, or authorization errors usually do not.
for ((attempt=1; attempt<=4; attempt++)); do
if fetch_artifact; then
break
fi
if (( attempt == 4 )); then
printf 'artifact fetch failed\n' >&2
exit 1
fi
sleep 2
done
Repeating an operation can duplicate side effects. Confirm that the operation or request is safe to retry.
6. Backoff reduces pressure on a failing dependency
delay=1
for ((attempt=1; attempt<=5; attempt++)); do
if transient_operation; then
break
fi
sleep "$delay"
((delay *= 2))
done
Later production lessons add maximum delays and jitter. The important concept is that repeated clients should not retry in tight synchronization.
7. Infinite loops should be deliberate
while true; do
collect_sample
sleep 60
done
A daemon-like loop also needs signal handling, cleanup, logging, and external supervision. An infinite loop is an architectural choice, not a default.
8. read naturally drives while loops
while IFS= read -r line; do
printf 'record=<%s>\n' "$line"
done < input.txt
The read command succeeds for each record and fails at
EOF, naturally ending the loop.
9. Preserve a final line without a newline when required
while IFS= read -r line || [[ -n $line ]]; do
printf 'record=<%s>\n' "$line"
done < input.txt
This matters when input does not guarantee a conventional final newline.
10. break and continue remain useful
while IFS= read -r service; do
[[ -z $service ]] && continue
[[ $service == STOP ]] && break
printf 'service=%s\n' "$service"
done < services.txt
11. Hands-on lab: bounded readiness simulation
mkdir -p "$HOME/devops-academy/bash/chapter05/lesson02"
cd "$HOME/devops-academy/bash/chapter05/lesson02"
rm -f ready.flag
max_attempts=5
attempt=1
while (( attempt <= max_attempts )); do
if [[ -f ready.flag ]]; then
printf 'READY attempt=%d\n' "$attempt"
break
fi
printf 'waiting %d/%d\n' "$attempt" "$max_attempts"
if (( attempt == 3 )); then
: > ready.flag
fi
((attempt += 1))
sleep 1
done
[[ -f ready.flag ]] || exit 1
Verification checklist
12. Knowledge check
Question 1. When does
while continue?
Question 2. When does until stop?
Question 3. Why should readiness polling be bounded?
Question 4. What must be considered before retrying a state-changing operation?
13. Summary
while and until loops are
condition-driven. In DevOps automation, their quality depends on
explicit timeout, retry, delay, and failure semantics—not merely
correct syntax.
14. Further reading
- GNU Bash Reference Manual — Looping Constructs.
- GNU Bash Reference Manual —
readbuiltin. - POSIX Shell Command Language — while and until.
- Google SRE material on retries and bounded waiting.
Keep the academy open
Support free, practical DevOps education.
Every lesson is designed to remain readable in a browser, downloadable from GitHub, and usable without a paid learning platform. Contributions help expand and maintain the curriculum.
0x716c4Ab160C4B66F31a28AE2448BfF68fc3a2ef0
Send only Ethereum/ERC-20 compatible assets to this
address.