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

Install Pip on Ubuntu 18.04: Python 3 and Python 2 Setup Guide

Pip is the official package manager for Python and the standard way to install libraries from…

3 days ago

Install R on Ubuntu 18.04 from CRAN: Statistical Computing Setup

R is an open-source programming language and environment built for statistical computing and data visualization. It…

3 days ago

Install Jenkins on Ubuntu 18.04: CI/CD Server Setup Guide

Jenkins is an open-source automation server that makes it easy to build CI/CD pipelines. Continuous integration…

3 days ago

Install Android Studio on Ubuntu 18.04 with Snap and OpenJDK 8

Android Studio is the official IDE for Android development, built on JetBrains' IntelliJ IDEA platform. It…

3 days ago

Install and Configure GitLab on Ubuntu 18.04 with Omnibus

GitLab is a web-based, open-source Git repository manager written in Ruby. It includes built-in tools for…

3 days ago

Install Anaconda on Ubuntu 18.04: Python Data Science Setup Guide

Anaconda is the most widely used Python distribution for data science and machine learning. It bundles…

4 days ago