Skip to content

Until Command on Linux (Loop - Until)

The until command on Linux is the opposite of the while command, but it executes something while the test is false, or with values other than zero.

Its syntax is:

until [CONDITION] of [COMMANDS] done

Example:

#! /bin/bash

counter=0
until [$counter -gt 3]
do
echo Counter: $counter
(counter++))
done When executed:

$. /counter

Counter: 0
Counter: 1
Counter: 2
Counter: 3 Until is useful for making a script wait until a command no longer returns an error, since it is only executed if the test return is different from zero.

A program’s zero exit code generally indicates that it ran successfully.

In this example, the until command is used to wait until the host is able to ping the IP 8.8.8.8. When the ping command succeeds in pinging host 8.8.8.8, it will return zero, causing the processing to leave the loop:

#! /bin/bash <br></br>until ping -c 1 8.8.8.8 &>/dev/null <br></br>from the <br></br>echo “Waiting for the network...”

sleep 1
done
echo “The network is on the air” Learn much more about Linux in our online course. You can register here. If you already have an account, or want to create one, just log in or create your user here.

Did you like it?

Share