How To

Bash While Loop Explained With Examples

Introduction

A Bash while loop is used to repeat commands as long as a condition is true. It is one of the most useful loop structures in Bash scripting. If you want a script to keep running until a certain condition changes, the while loop is the right choice.

Bash while loops are commonly used in Linux automation, system monitoring, cybersecurity scripts, log reading, countdown timers, menu scripts, and file processing. For beginners, learning the while loop helps you understand how Bash scripts can repeat actions based on conditions.

What Is A While Loop In Bash?

A while loop checks a condition before running commands. If the condition is true, the commands inside the loop are executed. After that, the condition is checked again. This continues until the condition becomes false.

Basic syntax:

while [[ condition ]]do    commanddone

The loop starts with while and ends with done.

Basic Bash While Loop Example

Create a new Bash script:

nano while-loop.sh

Add the following code:

#!/bin/bashcount=1while [[ $count -le 5 ]]do    echo "Count is: $count"    ((count++))done

Save the file and run it:

chmod +x while-loop.sh./while-loop.sh

Output:

Count is: 1Count is: 2Count is: 3Count is: 4Count is: 5

In this example, the loop continues while the value of count is less than or equal to 5.

Bash While Loop With User Input

You can use a while loop to repeat actions until the user enters a specific value.

#!/bin/bashanswer=""while [[ "$answer" != "exit" ]]do    read -p "Type exit to stop: " answer    echo "You typed: $answer"done

This script keeps asking for input until the user types exit.

Read A File Using While Loop

The while loop is commonly used to read files line by line.

#!/bin/bashwhile read linedo    echo "Line: $line"done < file.txt

This script reads each line from file.txt and prints it.

Cybersecurity Example: Monitor Failed SSH Logins

A while loop can also be useful for simple monitoring tasks.

#!/bin/bashwhile truedo    grep "Failed password" /var/log/auth.log | tail -5    sleep 10done

This script checks failed SSH login attempts every 10 seconds. Press CTRL + C to stop it.

Infinite While Loop

An infinite loop runs forever until manually stopped.

#!/bin/bashwhile truedo    echo "Monitoring system..."    sleep 5done

Infinite loops are useful for monitoring scripts, but use them carefully.

Conclusion

The Bash while loop is a powerful tool for repeating commands based on conditions. It is useful for user input, file reading, monitoring, automation, and cybersecurity scripting.

For beginners, understanding while loops is an important step in Bash scripting. Once you learn it, you can create smarter scripts that continue running until a task is completed or a condition changes.

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…

16 minutes 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…

5 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…

6 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…

7 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…

8 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…

9 hours ago