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.
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.
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.
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.
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.
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.
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.
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.
Introduction A self-signed SSL certificate is a certificate that is created and signed by the…
Introduction Debugging is an important part of Bash scripting. When a script does not work…
Introduction Cron jobs are used in Linux to run commands or Bash scripts automatically at…
Introduction Pipes are an important feature in Linux and Bash scripting. A pipe allows you…
Introduction The grep, awk, and sed commands are powerful text-processing tools in Linux. They are…
Introduction Working with files and directories is one of the most important skills in Bash…