Skip to main content

How to Repeat Commands Until a Condition Is True in bash and zsh with until

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Some jobs repeat until something changes: retry until a server answers, wait until a file appears, count up to a limit. The shell's until loop runs a block of commands again and again until a test succeeds. This guide counts three attempts, with real output from zsh 5.9 and bash 3.2.

Syntax​

Syntax
until <test-command>; do
<commands>
done
PartWhat it means
<test-command>Runs before each pass. The loop stops as soon as it succeeds, that is, ends with exit status 0
<commands>The commands to repeat. Something in them must change the test's result, or the loop never ends
doneMarks the end of the loop

The bash manual: "Execute consequent-commands as long as test-commands has an exit status which is not zero." The zsh manual says the same: "Execute the do list as long as until list returns a nonzero exit status."

Prerequisites​

  • zsh or bash. This guide uses zsh 5.9, the default shell on macOS, and checks every command in bash 3.2 too.

Step 1: Count Three Attempts​

Terminal
attempt=1
until [ $attempt -gt 3 ]; do
echo "Attempt $attempt"
attempt=$((attempt + 1))
done
Output
Attempt 1
Attempt 2
Attempt 3

zsh running the until loop: the until> prompts while it was typed, then Attempt 1, Attempt 2 and Attempt 3.

  • attempt=1: creates a variable named attempt that holds 1.
  • [ $attempt -gt 3 ]: the test. -gt means "greater than": the bash manual lists -eq, -ne, -lt, -le, -gt and -ge as the tests that compare numbers.
  • attempt=$((attempt + 1)): adds 1 each pass. After the third pass, attempt is 4, the test succeeds, and the loop ends.

While you type the loop, zsh starts each new line with until>, and bash with >, until done completes it. bash printed the same three lines.

Common Mistakes​

MistakeWhat happensDo this instead
Writing > for "greater than": [ $attempt > 3 ]> is a redirection, not a comparison: the loop printed nothing, and a file named 3 appeared in the folderUse -gt, -lt, -ge or -le inside [ ]
Never changing the variable in the loopThe test never succeeds, so the loop never endsChange it inside the loop, as attempt=$((attempt + 1)) does

Troubleshooting​

SymptomLikely causeFix
The loop never endsThe test never succeedsPress Ctrl+C: it stopped an endless until false loop at once

FAQ​

What's the difference between until and while?​

until repeats while the test fails; while repeats while it succeeds. while [ $attempt -le 3 ], with the same body, printed the same three lines.

Can until wait for something to happen?​

Yes. Any command can be the test. [ -f ready.txt ] succeeds once a file named ready.txt exists; the bash manual: "True if file exists and is a regular file."

Terminal
until [ -f ready.txt ]; do
echo "Waiting for ready.txt"
sleep 1
done

While it waited, a second terminal in the same folder created the file with touch ready.txt, and the loop ended:

Output
Waiting for ready.txt
Waiting for ready.txt
Waiting for ready.txt
Waiting for ready.txt

Conclusion​

until [ $attempt -gt 3 ]; do ... done ran its block three times and stopped once attempt reached 4. Compare numbers with -gt and its siblings inside [ ], and change something in the loop so the test can succeed.

Additional Resources​