An until loop in Bash is similar to a while loop, but it works in the opposite way. It repeatedly executes a block of commands until a specified condition becomes true. In other words, the loop continues as long as the condition is false and stops when the condition becomes true.

Basic Syntax

The basic syntax of an until loop in Bash is:

1
2
3
4
until [ CONDITION ]
do
# Commands to be executed
done
  • CONDITION is an expression that is checked before each iteration of the loop. If the condition is false, the loop body (the commands between do and done) is executed. If the condition becomes true, the loop exits.

    Example 1: Counting with an Until Loop

    Here’s an example that counts from 1 to 5 using an until loop:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    #!/bin/bash

    counter=1

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

Output:

1
2
3
4
5
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 5
  • In this example, the loop continues until the counter is greater than 5. The condition [ $counter -gt 5 ] becomes true when counter reaches 6, causing the loop to stop.

    Example 2: Waiting for a Condition to Become True

    You can use an until loop to wait for a condition to become true, such as waiting for a service to start:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    #!/bin/bash

    until pgrep apache2 > /dev/null
    do
    echo "Waiting for Apache server to start..."
    sleep 1
    done

    echo "Apache server is running!"
  • The loop will continue to check if the Apache server is running by using the pgrep command. Once pgrep finds the process and returns true, the loop exits.

    Example 3: Using an Until Loop for User Input

    Here’s an example where an until loop is used to prompt the user until they enter a specific input:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    #!/bin/bash

    password="secret"

    until [ "$user_input" = "$password" ]
    do
    read -p "Enter the password: " user_input
    done

    echo "Correct password entered!"
  • The loop will keep asking for the password until the user enters “secret”.

    Example 4: Infinite Loop with Until

    Although less common, you can create an infinite loop with until by using a condition that never becomes true:

    1
    2
    3
    4
    5
    6
    7
    #!/bin/bash

    until false
    do
    echo "This is an infinite loop. Press Ctrl+C to stop."
    sleep 2
    done
  • This loop will run indefinitely because false is always false.

    Summary

  • An until loop is similar to a while loop but continues running as long as the condition is false.

  • It’s useful when you want to repeat a block of code until a certain condition is met.

  • Typical use cases include waiting for a specific event to occur or processing input until a correct response is received.

Understanding until loops adds another useful tool to your Bash scripting toolkit, particularly for situations where you want to keep looping until a specific condition is satisfied.