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

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

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

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

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

19 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