until

shell builtinLinux/Unix
The until command is one of the most frequently used commands in Linux/Unix-like operating systems. until Execute commands until a condition is true

Quick Reference

Command Name:

until

Category:

shell builtin

Platform:

Linux/Unix

Basic Usage:

until [options] [arguments]

Common Use Cases

    Syntax

    until test-commands; do consequent-commands; done

    Options

    The until command is a shell construct rather than a standalone command with options. It doesn't have traditional command-line options like other commands. Instead, it has a specific syntax structure:

    until test-commands
    do
      consequent-commands
    done
    

    Where:

    • test-commands: Commands that determine whether the loop continues. The loop continues until these commands return a zero exit status (success).
    • consequent-commands: Commands that are executed on each iteration of the loop as long as the test-commands return a non-zero exit status (failure).

    The until loop is the logical opposite of the while loop. While a while loop executes as long as the condition is true, an until loop executes as long as the condition is false.

    Examples

    How to Use These Examples

    The examples below show common ways to use the until command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    # Basic Examples Basic
    # Wait until a file exists until [ -f /tmp/finish.txt ]; do echo "Waiting for file..." sleep 5 done echo "File exists!"
    # Wait for a process to finish until ! pgrep -x "myprocess" > /dev/null; do echo "Process is still running..." sleep 10 done echo "Process has finished."
    # Advanced Examples Advanced
    # Retry a command until it succeeds until ping -c 1 google.com > /dev/null; do echo "Network not available, waiting..." sleep 5 done echo "Network is up!"
    # Wait for a service to become available until nc -z localhost 8080; do echo "Waiting for service on port 8080..." sleep 2 done echo "Service is up!"
    # Implement a timeout mechanism start_time=$(date +%s) timeout=300 # 5 minutes until [ -f /var/lock/myapp.lock ] || [ $(($(date +%s) - start_time)) -gt $timeout ]; do echo "Waiting for lock file (timeout in $((timeout - ($(date +%s) - start_time))) seconds)..." sleep 5 done if [ -f /var/lock/myapp.lock ]; then echo "Lock file appeared!" else echo "Timed out waiting for lock file!" fi # Wait for user input until read -p "Continue? (y/n): " choice && [ "$choice" = "y" ]; do echo "Please answer y to continue." done # Monitor a log file until a pattern appears until grep -q "ERROR" /var/log/myapp.log; do echo "Monitoring log file for errors..." sleep 10 done echo "Error detected in log file!" # Wait for database connection until mysql -u user -ppassword -e "SELECT 1" 2>/dev/null; do echo "Waiting for database connection..." sleep 3 done echo "Database is accessible!" # Combined with counter to limit attempts counter=0 max_attempts=5 until [ $counter -ge $max_attempts ] || command_to_try; do counter=$((counter+1)) echo "Attempt $counter failed. Retrying in 5 seconds..." sleep 5 done if [ $counter -ge $max_attempts ]; then echo "Maximum attempts reached. Giving up." else echo "Command succeeded on attempt $counter." fi

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    The `until` command is a shell control structure in Unix and Linux shells, including Bash, that allows you to create loops that execute until a specified condition becomes true. It's essentially the inverse of the `while` loop: where `while` executes as long as a condition is true, `until` executes as long as a condition is false. This command structure is particularly useful in scripting scenarios where you need to wait for a specific event or condition to occur before proceeding with the rest of the script. The basic syntax is: ```bash until test-commands do consequent-commands done ``` The test-commands are evaluated before each iteration of the loop. If they return a non-zero exit status (indicating failure or "false" in shell logic), the consequent-commands are executed. This cycle repeats until the test-commands return a zero exit status (indicating success or "true"), at which point the loop terminates, and the script continues with any commands following the `done` statement. **Key characteristics and use cases of the `until` loop:** 1. **Waiting for Resources**: One of the most common uses is to wait for a resource to become available, such as a file, network service, or database connection. 2. **Polling**: The `until` loop is ideal for implementing polling mechanisms, where a script periodically checks for a condition and acts when the condition is met. 3. **Retry Logic**: It's useful for implementing retry mechanisms for commands that might fail temporarily but are expected to eventually succeed. 4. **User Interaction**: The loop can be used to repeatedly prompt a user for valid input until they provide it. 5. **System Monitoring**: Scripts can use `until` loops to monitor system status and react when certain thresholds are crossed or conditions are met. When using `until` loops, it's often a good practice to incorporate some form of timeout mechanism or maximum attempt limit to prevent infinite loops in case the expected condition never occurs. This can be achieved by maintaining a counter inside the loop or by calculating elapsed time. Another important consideration is the frequency of checks. In many cases, it's appropriate to include a `sleep` command within the loop to prevent excessive resource usage from rapid, continuous checking. The appropriate sleep duration depends on the specific scenario - shorter for time-sensitive operations, longer for operations where immediate response isn't critical. The `until` loop is available in most Unix-like shells, including Bash, Ksh, Zsh, and others, making it a portable and widely available tool for shell scripting. Its simple syntax and intuitive behavior make it an essential construct for creating robust, responsive shell scripts.

    Related Commands

    These commands are frequently used alongside until or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the until command works in different scenarios.

    $ until
    View All Commands