Bash until Loop

By 

Updated on

4 min read

Terminal showing a Bash until loop counting from zero to five

The until loop in Bash executes a set of commands repeatedly as long as a given condition evaluates to false. Once the condition becomes true, the loop stops.

In Bash scripting, there are three basic loop constructs: the for loop , the while loop , and the until loop. The until loop is the inverse of while — it runs while the condition is false rather than true.

This guide explains the syntax of the until loop and demonstrates its use with practical examples.

Syntax

The Bash until loop takes the following form:

sh
until [CONDITION]
do
  [COMMANDS]
done

The condition is evaluated before each iteration. If it evaluates to false (non-zero exit status), the commands inside the loop body run. If it evaluates to true (zero exit status), the loop terminates and control passes to the next command after done.

Basic Example

In the following example, the loop prints the current value of counter and increments the variable by one on each iteration. The condition uses the -gt comparison operator to check if counter is greater than 5:

sh
#!/bin/bash

counter=0

until [ $counter -gt 5 ]
do
  echo Counter: $counter
  ((counter++))
done

The loop runs as long as counter is not greater than 5. Once counter reaches 6, the condition [ 6 -gt 5 ] evaluates to true and the loop stops. The output is:

output
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 5

Use the break and continue statements to control the loop execution.

Practical Examples

Waiting for a Git Host

The following script retries git pull until it succeeds. This is useful when a git host has downtime and you do not want to keep typing the command manually:

sh
#!/bin/bash

until git pull &> /dev/null
do
    echo "Waiting for the git host ..."
    sleep 1
done

echo -e "\nThe git repository is pulled."

The sleep command pauses for one second between attempts. Once the repository is pulled successfully, the script prints a confirmation message:

output
Waiting for the git host ...
Waiting for the git host ...
Waiting for the git host ...

The git repository is pulled.

Waiting for a Service to Start

This script waits until a service is listening on a specific port before continuing:

sh
#!/bin/bash

echo "Waiting for port 3306 to become available..."

until nc -z localhost 3306 2>/dev/null
do
    sleep 1
done

echo "MySQL is ready."

Retry with Maximum Attempts

To avoid an infinite loop, you can add a retry counter that exits after a set number of attempts:

sh
#!/bin/bash

max_retries=10
attempt=0

until curl -s http://localhost:8080/health > /dev/null
do
    ((attempt++))
    if [ $attempt -ge $max_retries ]; then
        echo "Service did not start after $max_retries attempts."
        exit 1
    fi
    echo "Attempt $attempt of $max_retries..."
    sleep 2
done

echo "Service is up."

Countdown Timer

A simple countdown using until:

sh
#!/bin/bash

seconds=10

until [ $seconds -eq 0 ]
do
    echo "$seconds..."
    ((seconds--))
    sleep 1
done

echo "Done!"

Quick Reference

For a printable quick reference, see the Bash cheatsheet .

PatternDescription
until [ condition ]; do ... doneLoop while condition is false
until command; do ... doneLoop until command succeeds (exit 0)
until false; do ... doneInfinite loop (use break to exit)
breakExit the loop immediately
continueSkip to the next iteration
until [[ $var =~ regex ]]Loop until a regex matches

FAQ

What is the difference between while and until? The while loop runs as long as the condition is true. The until loop runs as long as the condition is false. They are logical inverses of each other: until [ condition ] is equivalent to while [ ! condition ].

Can I use until with command exit status instead of a test condition? Yes. The until loop checks the exit status of whatever follows until. Any command that returns a non-zero exit status (failure) keeps the loop running. When the command returns zero (success), the loop stops.

How do I create an infinite until loop? Use until false; do ... done. The false command always returns a non-zero exit status, so the condition never becomes true. Use break inside the loop to exit when needed.

How do I avoid an infinite loop when waiting for something? Add a retry counter that increments on each iteration and use break or exit when the maximum number of attempts is reached. See the Retry with Maximum Attempts example above.

Conclusion

The until loop repeats commands as long as a condition is false, making it ideal for waiting on external events like service availability or network connectivity. For the inverse behavior, use the while loop .

Tags

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page