[email protected]Office hours: Mon – Fri 9:00AM – 5:00PMLinkedInXFacebook
NexonHost
Netherlands Dedicated ServersAmsterdam · Equinix AM6Germany Dedicated ServersFrankfurt · Equinix FR4Romania Dedicated ServersBucharest · VoxilityAustria Dedicated ServersVienna · Interxion VIEBulgaria Dedicated ServersSofia · TelepointFrance Dedicated ServersParis · Interxion PAR — Marseille · Digital Realty MRS3Ireland Dedicated ServersDublin · Equinix DB2Italy Dedicated ServersMilan · Irideos AvalonPoland Dedicated ServersWarsawSpain Dedicated ServersMadrid · Equinix MD2United States Dedicated ServersAshburn · Equinix DC
All articles
Getting startedMay 31, 2023

Bash until Loop

Bash Until Loop

This tutorial explains the basics of the until loop in Bash.

Loops are one of the fundamental concepts of programming languages. Loops are handy when you want to run a series of commands over and over again until a specific condition is met.

In scripting languages such as Bash, loops are useful for automating repetitive tasks. There are 3 basic loop constructs in Bash scripting, for loop , while loop , and until loop.

 

Bash until Loop

The until loop is used to execute a given set of commands as long as the given condition evaluates to false.

The Bash until loop takes the following form:

until [CONDITION]
do
  [COMMANDS]
done

The condition is evaluated before executing the commands. If the condition evaluates to false, commands are executed. Otherwise, if the condition evaluates to true the loop will be terminated and the program control will be passed to the command that follows.

In the example below, on each iteration the loop prints the current value of the variable counter and increments the variable by one.

#!/bin/bash

counter=0

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

The loop iterates as long as the counter variable has a value greater than four. The script will produce the following output:

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

Use the break and continue statements to control the loop execution.

 

Bash until Loop Example

The following script may be useful when your git host has downtime, and instead of manually typing git pull multiple times until the host is online, you can run the script once. It will try to pull the repository until it is successful.

#!/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 print “Waiting for the git host …” and sleep for one second until the git host goes online. Once the repository is pulled, it will print “The git repository is pulled.”.

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

The git repository is pulled.

 

Conclusion

The while and until loops are similar to each other. The main difference is that the while loop iterates as long as the condition evaluates to true and the until loop iterates as long as the condition evaluates to false.

Keep reading
How to Implement Network Segmentation and Resource Isolation in Linux.
February 6, 2024

How to Implement Network Segmentation and Resource Isolation in Linux.

How to Implement Network Segmentation and Resource Isolation in Linux. Securing and managing networks in a Linux environment involves advanced practices like network segmentation and resource isolation. This guide explores these concepts, providing detailed instructions and examples for…

Read article
How to open or close ports in Linux.
February 6, 2024

How to open or close ports in Linux.

How to open or close ports in Linux. Effectively managing ports on your Linux system is crucial for both security and service accessibility. This comprehensive guide will provide a detailed walkthrough on opening and closing ports using the robust tools iptables and ufw. Understanding these…

Read article
How to use ypdomainname command.
January 23, 2024

How to use ypdomainname command.

How to use ypdomainname command. In the realm of Linux networking, the ypdomainname command plays a crucial role in the context of the Network Information Service (NIS). NIS, formerly known as Yellow Pages, is a distributed database service that facilitates the sharing of network configuration and…

Read article
Get in Touch

Together, Let’s Build a Faster, Safer Internet.

Build scalable infrastructure with NexonHost — high-performance dedicated servers, VPS hosting, colocation, and DDoS protection across Europe.

Get Started →Contact Us
[email protected]Office hours: Mon – Fri 9:00AM – 5:00PM