Bash Until Loop

Introduction

One of the most fundamental ideas in programming languages is the loop. When you want to perform a collection of commands multiple times until a specific condition is met, loops come in handy.

Loops are useful for automating repeated operations in scripting languages like Bash. In Bash scripting, there are three basic loop constructs: for loop, while loop, and until loop.

In this tutorial, we have explained until loop in Bash. We will also address a few FAQs on Bash Until Loop Command.

Bash until Loop

The until loop is used to run a collection of commands until the supplied condition returns false.

The Bash until loop is written as follows:

until [CONDITION]
do
  [COMMANDS]
done

Before the commands are executed, the condition is evaluated. Commands are executed if the condition evaluates to false. If the condition evaluates to true, the loop will be terminated and control of the program will be given to the next instruction.

The loop in the example below prints the current value of the variable counter and increases the variable by one with each iteration.

#!/bin/bash

counter=0

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

The loop will continue to iterate as long as the counter variable is bigger than four. The following is the output of the script:

Output

Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 5

To manage the loop's execution, use the break and continue statements.

Bash until Loop Example

When your git host is down, instead of entering git pull many times till the host comes back online, you can run the script once. It will keep attempting to pull the repository until it succeeds.

#!/bin/bash

until git pull &> /dev/null
do
    echo "Waiting for the git host ..."
    sleep 1
done

echo -e "\nThe git repository is pulled."

The script will output "Waiting for the git host..." and sleep for one second while waiting for the git host to come online. It will output "The git repository is pulled." once the repository has been pulled.

Waiting for the git host ...
Waiting for the git host ...
Waiting for the git host ...

The git repository is pulled.

FAQs on Bash Until Loop

Can I use variables in the until loop's condition? 

Yes, you can use variables in the until loop's condition. The loop continues executing as long as the condition evaluates to false.

How is the until loop different from the while loop? 

The until loop continues executing as long as the condition evaluates to false, whereas the while loop executes as long as the condition evaluates to true. They have opposite conditions.

Can I exit or terminate an until loop prematurely? 

Yes, you can exit or terminate an until loop prematurely by using the break statement. When encountered, the break statement transfers control to the first line after the loop.

How can I skip the rest of the current iteration in an until loop? 

To skip the remaining code in the current iteration and start the next iteration immediately, you can use the continue statement. However, the continue statement is not commonly used with until loops.

Can I use multiple conditions in an until loop? 

Yes, you can use multiple conditions in an until loop by combining them using logical operators such as && (AND) or || (OR). The loop continues until all conditions evaluate to true (with &&).

Can I use a nested until loop in Bash? 

Yes, you can use nested until loops in Bash. Nested loops allow you to perform more complex iterations or repeated tasks within each iteration of another loop.

Can I modify the looping variable inside the until loop? 

Yes, you can modify the looping variable inside the until loop. However, it's important to ensure the loop's termination condition eventually evaluates to true to prevent infinite loops.

Conclusion

The while and until loops are nearly identical. The primary distinction is that the while loop iterates until the condition evaluates to true, whereas the until loop iterates until the condition evaluates to false.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.