until
Execute commands repeatedly until condition is true
TLDR
Execute a command until it succeeds
Wait for a systemd service to be active
SYNOPSIS
until test_command; do
commands
done
or in a single line:
until test_command; do commands; done
PARAMETERS
test_command
A command or list of commands whose exit status is evaluated. The loop continues as long as this command returns a non-zero (failure) exit status. The loop terminates when it returns zero (success).
commands
One or more commands that are executed repeatedly as long as the test_command returns a non-zero exit status.
DESCRIPTION
The until construct is a fundamental control flow statement in shell scripting, offering a powerful way to manage iterative processes. It repeatedly executes a block of commands as long as a specified test command returns a non-zero (failure) exit status. The loop continues its iterations, re-evaluating the test command before each pass of the command block. The loop terminates, and execution continues with the command following the done keyword, once the test command successfully returns an exit status of zero. This behavior makes it the logical inverse of the while loop. It's particularly useful for scenarios where you need to wait for a specific condition to become true, such as a file appearing or a service becoming available, before proceeding with subsequent operations. The test command is always executed at the beginning of each potential iteration; if it succeeds on the very first evaluation, the commands within the loop are never executed.
CAVEATS
until is a shell keyword, not an external executable command. Its behavior is intrinsic to the shell (e.g., Bash, Zsh, Dash). The test_command is always executed at least once before the loop body potentially runs. If the test_command succeeds immediately, the loop body will never execute. Be careful to ensure the test_command eventually succeeds to prevent infinite loops.
EXIT STATUS
The behavior of the until loop is entirely dependent on the exit status of the test_command. An exit status of 0 signifies success, while any non-zero value indicates failure. This is a common convention in Unix-like systems, where commands return 0 upon successful execution and non-zero for various errors or conditions.
LOOP CONTROL KEYWORDS
Within an until loop, the break and continue keywords can be used to alter the flow of execution. break immediately terminates the loop and continues execution with the command following done. continue skips the rest of the current iteration and proceeds to the next iteration, re-evaluating the test_command.
PRACTICAL EXAMPLE
A common use case for until is to wait for a service to become available or a file to appear. For example:
until ping -c 1 example.com &>/dev/null; do
echo "Waiting for example.com to be reachable..."
sleep 5
done
echo "example.com is now reachable!"
HISTORY
The until loop construct has been a standard feature of Unix shells since their early development, providing a fundamental control flow mechanism alongside while and for loops. It's part of the POSIX standard for shell command language, ensuring its presence and consistent behavior across compliant shells like Bash, Zsh, and Dash.