Until Command in Linux: Create Loops Until Condition is True
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
That was just one piece. The course teaches the whole path — from your first command to exam day.