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

Best OSINT Tools for Journalists 2026: Verify Sources, Images and Claims

Journalists use OSINT to verify public information before publishing. In 2026, misinformation, AI-generated images, fake…

32 minutes ago

Install Docker on Ubuntu 20.04: Complete Step-by-Step Guide

DockerĀ is an open-source platform that lets you package and run applications inside containers. Each container…

11 hours ago

Install PostgreSQL on Ubuntu: Database Setup and Admin Guide

PostgreSQL (often called Postgres) is an open-source relational database system. It supports advanced features like JSON…

12 hours ago

Install Xrdp Remote Desktop on Ubuntu: Setup and Connect

Xrdp is an open-source server that lets you connect to your Ubuntu machine from another computer…

12 hours ago

Tomcat 9 on Ubuntu 20.04: Install, Configure, and Start

Apache Tomcat is an open-source web server and Java servlet container. It is one of the…

12 hours ago

Automatic Updates on Ubuntu: Set Up unattended-upgrades

Keeping your Ubuntu system updated is one of the best ways to protect it. Security…

13 hours ago