The Bash until loop is used to repeat commands until a condition becomes true. It is similar to the while loop, but it works in the opposite way. A while loop runs while a condition is true, but an until loop runs while a condition is false.
If you are learning Bash scripting, understanding the until loop is useful for automation, retry scripts, countdown timers, service checking, user input validation, and cybersecurity monitoring tasks. It helps you write scripts that continue running until a required result is achieved.
An until loop keeps executing commands until the given condition becomes true.
Basic syntax:
until [[ condition ]]do commanddone
The loop starts with until and ends with done.
In simple words, Bash checks the condition first. If the condition is false, the loop runs. When the condition becomes true, the loop stops.
Create a new Bash script:
nano until-loop.sh
Add the following code:
#!/bin/bashcount=1until [[ $count -gt 5 ]]do echo "Count is: $count" ((count++))done
Save the file and run it:
chmod +x until-loop.sh./until-loop.sh
Output:
Count is: 1Count is: 2Count is: 3Count is: 4Count is: 5
In this example, the loop runs until count becomes greater than 5.
You can use an until loop to keep asking for input until the user enters the correct value.
#!/bin/bashpassword=""until [[ "$password" == "admin123" ]]do read -p "Enter password: " passworddoneecho "Access granted"
This script keeps asking for a password until the user enters admin123.
The until loop is useful when waiting for a file to exist.
#!/bin/bashfile="/tmp/testfile.txt"until [[ -f "$file" ]]do echo "Waiting for file..." sleep 5doneecho "File found: $file"
This script checks every 5 seconds until the file is created.
You can use an until loop to check whether a host becomes reachable.
#!/bin/bashhost="192.168.1.1"until ping -c 1 "$host" > /dev/null 2>&1do echo "$host is not reachable. Retrying..." sleep 3doneecho "$host is online"
This is useful in network monitoring and basic cybersecurity automation.
A while loop runs when the condition is true. An until loop runs when the condition is false.
Example idea:
while [[ $count -le 5 ]]
This runs while count is less than or equal to 5.
until [[ $count -gt 5 ]]
This runs until count becomes greater than 5.
The Bash until loop is a simple and useful loop for beginners. It repeats commands until a condition becomes true. You can use it for user input, file checking, retry tasks, network monitoring, and automation scripts.
For Bash scripting beginners, learning the until loop gives you more control over repeated tasks. Once you understand how it works, you can use it to create smarter Linux automation and cybersecurity scripts.
Introduction A self-signed SSL certificate is a certificate that is created and signed by the…
Introduction Debugging is an important part of Bash scripting. When a script does not work…
Introduction Cron jobs are used in Linux to run commands or Bash scripts automatically at…
Introduction Pipes are an important feature in Linux and Bash scripting. A pipe allows you…
Introduction The grep, awk, and sed commands are powerful text-processing tools in Linux. They are…
Introduction Working with files and directories is one of the most important skills in Bash…