Updated April 10, 2023
Introduction to While loop in Shell Scripting
In this article, we will learn about While loop in Shell Scripting. When we need to do the same task or perform the same operation then we need to write a program which does the work for one time and repeat the same program the number of times which we want to perform or we can call the same program again and again until the number of times. This calling can be done in many ways one of the ways is looping. While loop provides a way to execute the same program by checking a condition when the condition satisfies the program will execute otherwise it won’t execute and the process repeats until the condition fails to meet.
Syntax for While loop
The syntax for the while loop in shell scripting can be represented in different ways based on the use as below:
First Syntax Method
while [ condition ] do command1 command2 done
Explanation to the above syntax: In the syntax above, the condition is checked on a variable so that if the condition is satisfied the commands will be executed. We’ve got some built-in keywords in shell scripting, and while, do, and done, they’re among those. In the above syntax example, until the condition evaluates to true all the commands command1, command2 between do and done keywords will be executed and while loop will terminate when the condition is not satisfied and proceeds to statement next to the done keyword.
Second Syntax Method
If we want to check conditions on multiple lines of a text file then we can use the following syntax.
while IFS = read -r line do command on $line command1 on $line done < “file path”
Explanation to the above syntax: In the above syntax, while, do, and done are built-in keywords whereas IFS is used to set field separator which is by default a white space and -r indicates read mode. The above while loop will read line by line from the text file in the path specified and execute the commands on the current line until all lines in the text file are read and operations are performed on them. Once all the lines are finished than while loop will terminate
Third Syntax Method
If you want to perform some operation or execute a function infinite times then we can define while loop as below:
while : do command command1 done
Explanation to the above syntax: In the above while loop syntax method, there is no condition to check and perform operations based on the result of the condition. By default, the while condition evaluates to true always and commands in while loop will execute infinite times.
Flow Diagram for While loop
Let us consider an example for while loop and discuss the flow of the while loop using the example as below:
a=0 while [ $a -lt 10 ] do echo $a a = 'expr $a + 1' done
Explanation to the above code: In the above, while loop program, first we initialized the variable a with value 0 and we started while loop with condition a < 10 where -lt is less than so it checks the condition as it evaluates to true the body of the while loop will execute which echo $a means, it displays the value of a and next statement a = `expr $a + 1` which will increment the value of a by 1 and again condition will check and execute the body of the loop until the condition evaluates to false and program execution goes to the statement next to the end of the while loop. In the above while loop example, it will execute a total 10 times from 0 to 9 and during the 11 times the condition evaluates to false and the loop will terminate there.
How does While loop work in Shell Scripting?
While loop in shell script works in the following way, it is a control flow statement that provides a way to run a program or piece of code for a certain number of times. Before starting the while loop it checks the condition, if it evaluates to true then the body of the loop will execute and continues the same process until the condition evaluates to false. For every iteration condition is being checked, if it necessary it increases the variable in the condition and operations will perform on the variable too.
n = 1 while [ $n -le 5 ] do echo "Welcome $n times" n=$((n+1)) done
Explanation to the above code: In the above while loop example, we are checking whether the condition is < 5 as it evaluates to true the body of the loop will execute and display welcome value times where value is n. It will continue until the condition evaluates to false.
Examples to Implement in While loop in Shell Scripting
Let us discuss while loop with different examples and discuss what it is trying to do with that particular while loop and how we need to write and what things need to take care of while writing.
Let us write a while loop to read a text file line by line and the while loop program is below:
file_path= /etc/resolv.config while IFS = read -r line do echo $line done < "$file_path"
Explanation to the above code: In the above while loop example, we have initialized file_path variable with the location of the file and we are reading file line in the while loop condition, if we were able to read a line then the condition evaluates to true and then execute the body of while loop which is a display of the line here. So, while will continue execution until it reads all the lines from the file and then terminate the loop and the output of the above program will change based on the content of the file we are reading.
Let us have a look at how we can use while loop to run a program or display a program for an infinite number of times can be seen as below:
while : do echo " hello, need to enter ctrl+c to stop" done
Explanation to the above code: In the above example, there is no condition in the while loop so it always evaluates to true and body of the loop will execute continuously until we enter ctrl+c to stop the while loop. The echo statement will display infinite times until we press ctrl+c.
Let us write a while loop to calculate the factorial of a given number and display it on the console.
counter=$1 factorial=1 while [ $counter -gt 0 ] do factorial=$(( $factorial * $counter )) counter=$(( $counter - 1 )) done echo $factorial
Explanation to the above code: In the above program we are trying to calculate the factorial of a given number. We are assigning argument 1 to the counter variable and assigning factorial to value 1 and checking the condition whether the counter is greater than 0 if it satisfies then perform the operations in the body of the loop until the loop terminates and final displays factorial result.
Finally, it’s an overview of while loop in shell scripting. So far we have discussed what is while loop, while loop syntax, the flow diagram of the while loop, how while loop works in shell scripting, examples of while loop and its outputs. I hope you will have a better understanding of the while loop after reading this article.
We hope that this EDUCBA information on “While loop in Shell Scripting” was beneficial to you. You can view EDUCBA’s recommended articles for more information.