Until Command on Linux (Loop – Until) [Basic Guide]

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 
until ping -c 1 8.8.8.8 &>/dev/null
from the
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

Uirá Endy Ribeiro

Uirá Endy Ribeiro is a Software Developer and Cloud Computing Architect with a 23-year career. He has master's degrees in computer science and fifteen IT certifications and is the author of 11 books recognized in the IT world market. He is also Director at Universidade Salgado de Oliveira and Director of the Linux Professional Institute - LPI Director's Board.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


This site uses Akismet to reduce spam. Learn how your comment data is processed.

Need help?