← Back to Bash Course | Chapter 4: Loops | Lesson 8 of 14

until Loop

An until loop is the mirror image of while: it keeps repeating as long as its condition is false, stopping the moment the condition finally becomes true.

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

bash
#!/bin/bash
count=1
until [ "$count" -gt 5 ]; do
    echo "Count is $count"
    count=$((count + 1))
done
echo "Stopped once count exceeded 5"

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

bash
#!/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

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

bash
#!/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

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

bash
#!/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"
Common Mistakes
  1. Confusing until with while !; they are logically equivalent for simple conditions, but until reads more naturally when you're expressing 'keep going until X happens' rather than 'keep going while not X'.
  2. Forgetting that, like while, the condition is re-checked before every iteration -- not just once at the start.
  3. Writing an until condition 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).
Chapter Summary
  • Syntax: until condition; do ... done -- runs the body while condition is false, stopping once it becomes true.
  • until condition behaves the same as while ! 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.
  • until is often chosen for polling-style logic: 'keep trying until this succeeds'.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.