How To

Bash Until Loop Explained For Beginners

Introduction

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.

What Is An Until Loop In Bash?

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.

Basic Bash Until Loop Example

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.

Bash Until Loop With User Input

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.

Using Until Loop To Check A File

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.

Cybersecurity Example: Wait Until A Host Is Online

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.

Difference Between While And Until Loop

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.

Conclusion

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.

Cyber Defence

Recent Posts

groupdel Command in Linux: Remove a Group and Audit Files

The groupdel command in Linux removes a group from the system. It deletes the group's entry from /etc/group and /etc/gshadow,…

20 hours ago

wc Command in Linux: Count Lines, Words, Characters, and Bytes

The wc command in Linux counts lines, words, characters, and bytes in files or standard input. It…

20 hours ago

top Command in Linux: Monitor Processes and Resource Usage

The top command in Linux provides a real-time view of running processes and system resource usage. From…

20 hours ago

usermod Command in Linux: Modify User Accounts and Groups

The usermod command in Linux modifies existing user account attributes. You can use it to manage group…

20 hours ago

sort Command in Linux: Sort Text, Numbers, Columns, and More

The sort command in Linux reads lines from files or standard input and writes them to standard…

2 days ago

wall Command in Linux: Broadcast Messages to Logged-In Users

The wall command in Linux sends a message to the terminals of all currently logged-in users. The…

2 days ago