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

How To Create A Self-Signed SSL Certificate Using Bash And OpenSSL

Introduction A self-signed SSL certificate is a certificate that is created and signed by the…

1 minute ago

How To Debug Bash Scripts Using bash -x And set Commands

Introduction Debugging is an important part of Bash scripting. When a script does not work…

4 hours ago

How To Use Cron Jobs With Bash Scripts For Automation

Introduction Cron jobs are used in Linux to run commands or Bash scripts automatically at…

5 hours ago

How To Use Pipes In Bash Scripts For Command Chaining

Introduction Pipes are an important feature in Linux and Bash scripting. A pipe allows you…

6 hours ago

How To Use grep, awk, And sed In Bash Scripts

Introduction The grep, awk, and sed commands are powerful text-processing tools in Linux. They are…

7 hours ago

How To Work With Files And Directories Using Bash Scripts

Introduction Working with files and directories is one of the most important skills in Bash…

8 hours ago