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.
Both git fetch and git pull talk to a remote repository, but they do very different things to your…
Sometimes the change you need already exists, just on the wrong branch. A hotfix lands…
Email is still one of the most important communication channels inside modern applications. Password resets,…
Nginx is a high-performance web server and reverse proxy trusted by some of the largest…
ufw (Uncomplicated Firewall) sits on top of iptables (or nftables on newer systems) and replaces…
When you share a server with a team or investigate unexpected activity, the first question…