fbpx Until Command in Linux: Create Loops Until Condition is True | Certificação Linux

Until Command in Linux: Create Loops Until Condition is True

📅 12/07/2021 ⏱ 1 min ✍️ Uira Ribeiro
Linuxcommands
Until Command in Linux: Create Loops Until Condition is True
Exam Pass App — mais de 7.000 questões LPI, offline, em 5 idiomas, disponível no Android e na App Store
Available on
Google Play
Available on
App Store

The until command in Linux is the opposite of the while command, but it executes something while the test is false, or with non-zero values.

Its syntax is:

until [CONDIÇÃO]do  [COMANDOS]done

Example:

#!/bin/bashcounter=0until [ $counter -gt 3 ]do  echo Counter: $counter  ((counter++))done

When executed:

$ ./contador Counter: 0Counter: 1Counter: 2Counter: 3

The until is useful for making a script wait until a command no longer returns an error, since it only executes if the test return is different from zero.

A zero exit code from a program usually indicates that it executed successfully.

In this example, the until command is used to wait until the host can ping the IP 8.8.8.8. When the ping command successfully pings the host 8.8.8.8, it will return zero, causing the processing to exit the loop:

#!/bin/bashuntil ping -c 1 8.8.8.8 &>/dev/nulldo    echo "Esperando a rede..."    sleep 1doneecho "A rede está no ar"

Learn much more about Linux in our online course. You can enroll here. If you already have an account, or want to create one, just log in or create your user here.

Liked it? Share

Uira Ribeiro

Prof. Uirá Ribeiro

Chair do Board do Linux Professional Institute

After years of watching professionals waste hours on simple tasks because they didn't know the right Linux tools, I decided to teach.
Today I'm the author of most of LPI's official Learning Materials and mentor to over 14,000 certified students.

See my full story →

That was just one piece. The course teaches the whole path — from your first command to exam day.

Certificação Linux — a plataforma líder em certificações Linux no Brasil
WhatsApp