until

The until keyword in Bash is used to execute a block of code repeatedly until a certain condition is met. The syntax for the until keyword is as follows:

until condition; do
    commands
done

The condition is a Boolean expression that must evaluate to false for the block of code to be executed repeatedly. The commands are the commands that will be executed repeatedly.

For example, the following code will print the numbers from 1 to 10, but will stop if the number is equal to 5:

i=1

until [ $i -eq 5 ]; do
    echo $i
    i=$((i+1))
done

The until keyword is a powerful tool that can be used to automate repetitive tasks. It can also be used to test for certain conditions.

Here are some other examples of how the until keyword can be used:

  • To print the numbers from 1 to 100, but to skip any numbers that are divisible by 5:

i=1

until [ $i -eq 100 ]; do
    if [ $((i % 5)) -ne 0 ]; then
        echo $i
    fi
    i=$((i+1))
done
  • To check if a file exists:

file_name="myfile.txt"

until [ -f $file_name ]; do
    echo "The file does not exist yet."
    sleep 1
done

echo "The file now exists."

The until keyword is a versatile tool that can be used to automate repetitive tasks and to test for certain conditions. It is a valuable tool for any Bash script.

Last updated