Bash programming comprises three kinds of loops: the for loop, the while loop, and the until loop. The idea of all the loops is to repeatedly execute some code as long as some specific parameters are met.
The Bash for loop operates differently from the for loops in other programming languages you may be familiar with. With it, you can iterate over a series of “words” in a string.
On the other hand, the while loop executes the code under it if the control expression (or simply, the “condition”) is true. The loop stops executing the code when the condition becomes false, or there is an explicit break in the code.
The until loop is comparable to the while loop, with the only difference being that the code keeps executing while the control expression is deemed false.
In this post, we discuss how to use the Bash while loop with examples.
Understanding the While Loop Syntax
Take a look at the simple syntax of the while loop:
while [ condition ] |
The beginning and end of a while loop are defined by the do and done keywords, respectively. The execution/termination condition is defined inside the square brackets at the beginning of the loop.
Examples of Bash While Loop
To run the examples below, create a Bash file with the .sh extension and paste the code inside it. You can then run the code by typing “bash” followed by the filename (for example, “whileloop.sh”) in the terminal and hitting enter.
#1 Iterating a Fixed Number of Times
#!/bin/bash # Initializing a counter variable |
The above while loop iterates over the code block three times and prints the statement every time. Running the script will generate the following outcome:
Iteration count: 1 |
#2 Using the “Break” Statement
The break statement allows you to terminate the loop if a specific condition is met. Create a Bash file with the following code:
#!/bin/bash # Initializing a counter variable |
The above while loop iterates over the code three times, but the counter value is only printed two times since the break statement terminates the loop when the counter reaches value “3.” Running the script will generate the following outcome:
Iteration number 1 |
#3 Removing a Step with the Continue Statement
#!/bin/bash # Initializing the counter # Check counter value done |
The above code will iterate through the loop four times, but when the counter value reaches three, the continue value skips the rest of the code, jumps back to the beginning, and checks the condition.
The output for the code is:
Iteration number 1 |
#4 Reading a File
#!/bin/bash # Checking whether a command-line argument is provided # Assigning the filename from the provided value # Reading the file |
Assuming that you paste this code in a file named “ReadingFileWithWhile.sh,” running the command “bash ReadingFileWithWhile.bash” will return “No argument provided.”
However, if you run the same command and pass a text file to it, the output will look like this:
$ bash ReadingFileWithWhile.bash names.txt |
#5 Writing Text into a File
The following code will ask for a filename from the user and write the provided text to it:
#! /bin/bash echo -n “Enter the name of the file you want to create: “ |
To end the loop reading the content, you must press Ctrl+D.
#6 Creating an Infinite Loop
Writing an infinite loop can be a suitable solution for many problems. Making an infinite while loop is as simple as not mentioning a termination condition. However, you will need to use an exit statement to stop the loop as necessary.
The following loop will iterate six times, and when the value becomes six, the exit statement will stop the loop:
#!/bin/bash # Initialize the counter done |
The output of the script looks like this:
Iteration number: 1 |
Conclusion
In this guide, we discuss a handful of the different ways you can use while loops in Bash. There are other ways to use the while loop, and you can solve many different problems with it.
Now that you know the fundamentals of the while loop, you can implement it in your Bash scripts.