How to Use Bash While Loop Command to Automate Tasks Easily
Bash while loop is one of the three basic loops that are a part of this programming language. Just as the other two (until and for loop), this one can be useful when there is a need to repetitively run a series of commands until you meet a specific requirement.
Let’s learn more about how to use the Bash while loop and how it can be beneficial.
In short, the while loop can execute the required commands as long as the particular requirement is met.
Here is the basic syntax of the loop:
while [REQUIREMENT] do [COMMANDS] done
Think of the requirement as a condition that the loop checks against before performing the commands. The condition must be met so that the loop is executed. In cases of the system returning a false result on the requirement evaluation, the loop will immediately terminate. This is very similar to the bash for loop functions which we discussed a few weeks ago.
Let’s take a look at an example of the Bash while loop:
i=1 while [ $i -<= 5] do echo No: $i((i++)) done
The first line set the variable “i.” The following line determined the requirement that “i” shouldn’t be bigger than five.
Take a look at the output of the above command:
No: 1 No: 2 No: 3 No: 4 No: 5
At that point, the loop terminates, and the next command takes control of the program.
Infinite While Loop
As the name suggests, this loop is never terminated. You can make it by ensuring that the requirement is always true.
The trick in using this loop is utilizing the integrated “:” as it always results in a true evaluation. There are more examples of how to create an infinite while loop, but this one looks like this:
while : do echo "You can exit by pressing <CTRL+C>." Sleep 1 done
How to Read a File One Line At a time
The while loop has another common usage – reading a variable, data stream, or file one line at a time.
Here is an example:
file=file_path1 while read -r line; doecho $line done
In the first line, you should specify the path to the file you want to read. The loop will continue as long as there are lines in the file.
One thing to keep in mind is that the “read” command has a default setting to trim tabs and spaces. If you want to prevent that, here is how you should adjust the command:
file=file_path1 while IFS= read -r line; do echo $line done
You can use the break statement to execute loop termination once the set requirement is fulfilled.
Here is an example of using the break statement in one of the above commands:
i=1 while [ $i -<= 5] do echo No: $i ((i++)) If [[ "$i" == '3']]; then break fi done echo 'No more numbers!'
Although the loop is tasked to count all the numbers up to five, it will terminate when it reaches three. Therefore, the output is the following:
No: 1 No: 2 No more numbers!
The continue statement is excellent when you want to end the loop at some point and continue it with the next iteration.
Here is the same function as above, but with a continue statement:
i=1 while [ $i -<= 5] do echo No: $i ((i++)) If [[ "$i" == '3']]; then continue fi echo No: $i done echo 'No more numbers!'
In essence, we skipped one iteration, and the output looks like this:
No: 1 No: 2 No: 4 No: 5 No more numbers!
That rounds up our guide on how to use the Bash while loop. We hope that everything is clear, but in case you have any questions, we are here to answer them. You can also check out our bash function guide for more insight. Make sure to give it a shot first, and you will realize that mastering this loop is a lot simpler than you thought!
Best Linux Distros for Developers and Programmers as of 2022
Linux might not be the preferred operating system of most regular users, but it’s definitely the go-to choice for the majority of developers and programmers. While other operating systems can also get the job done pretty well, Linux is a more specialized OS that was…
How to Install Pip on Ubuntu Linux
If you are a fan of using Python programming language, you can make your life easier by using Python Pip. It is a package management utility that allows you to install and manage Python software packages easily. Ubuntu doesn’t come with pre-installed Pip, but here…