until Loop
In this page:
Basic until Loop
until condition; do ... done runs the body repeatedly as long as condition evaluates to false (non-zero exit status), stopping the first time it becomes true. This reads naturally for 'do this until some goal is reached'.
Example: Basic until Loop
#!/bin/bash
count=1
until [ "$count" -gt 5 ]; do
echo "Count is $count"
count=$((count + 1))
done
echo "Stopped once count exceeded 5"
Login to try C/C++/Java/PHP code in the editor
until vs while ! condition
until condition and while ! condition produce the same looping behavior for a simple command-based condition, since until is essentially while with the sense of the test inverted. Choosing between them is purely a matter of which one reads more clearly for the situation.
Example: until vs while ! condition
#!/bin/bash
n=0
until [ "$n" -ge 3 ]; do
echo "until: n=$n"
n=$((n + 1))
done
n=0
while ! [ "$n" -ge 3 ]; do
echo "while!: n=$n"
n=$((n + 1))
done
Login to try C/C++/Java/PHP code in the editor
Polling-Style Retry with until
A common real use of until is retrying an operation until it succeeds (or a maximum number of attempts is reached), which reads as 'keep retrying until success'. Combine it with a counter to avoid looping forever if the condition never actually becomes true.
Example: Polling-Style Retry with until
#!/bin/bash
attempt=0
succeeded=false
until [ "$succeeded" = true ] || [ "$attempt" -ge 3 ]; do
attempt=$((attempt + 1))
echo "Trying operation, attempt $attempt"
if [ "$attempt" -eq 2 ]; then
succeeded=true
fi
done
if [ "$succeeded" = true ]; then
echo "Succeeded on attempt $attempt"
else
echo "Gave up after $attempt attempts"
fi
Login to try C/C++/Java/PHP code in the editor
Condition That Starts True Skips the Body
Since until checks its condition before the very first iteration, if that condition is already true, the loop body never runs even once. Bash has no built-in do-while/do-until construct that guarantees at least one execution -- that pattern must be simulated manually if needed.
Example: Condition That Starts True Skips the Body
#!/bin/bash
already_done=true
until [ "$already_done" = true ]; do
echo "This line never prints"
done
echo "Loop body was skipped because the condition started true"
Login to try C/C++/Java/PHP code in the editor
- Confusing
untilwithwhile !; they are logically equivalent for simple conditions, butuntilreads more naturally when you're expressing 'keep going until X happens' rather than 'keep going while not X'. - Forgetting that, like
while, the condition is re-checked before every iteration -- not just once at the start. - Writing an
untilcondition that starts out already true, causing the loop body to never execute at all, which surprises people expecting at least one run (Bash has no built-in do-while/do-until construct).
- Syntax:
until condition; do ... done-- runs the body whileconditionis false, stopping once it becomes true. until conditionbehaves the same aswhile ! condition(for command exit-status conditions).- If the condition is already true before the first check, the body never runs at all -- there is no built-in do-until.
untilis often chosen for polling-style logic: 'keep trying until this succeeds'.
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: