Introduction to Loops in Shell Scripting
What is a loop? Why do we need it? If we want to perform an operation multiple times with a single variable change or based on a condition then we need to write the logic number of times. So, instead of that, we can use loops to perform an operation a certain number of times. In shell scripting, different types of loops are available to perform looping such as for loop, while loop, and until loop. These loops will execute commands iteratively until a condition satisfies and comes out of the loop when the condition is not satisfied. We can control the loops using some keywords in shell scripting and we can use loops to automate repetitive tasks.
Types of Loops in Shell Scripting
Below are the 3 different types of loops in shell scripting which are as follows:
1. While Loop
The while loop syntax in the shell scripting will be represented in the following way.
while [ condition ]
In the above syntax, initially, the condition will be evaluated if it is true then the commands inside the loop will execute until the condition remains true else it will come outside of the loop. Here while, do, and done are built-in keywords. It will execute all the commands while the condition evaluates to true.
While loop to display numbers from 1 to 10.
number = 1
while [ $number –lt 11 ] do
Code Explanation: In the above example, we are trying to display numbers from 1 to 10. The statement number =1 is initializing the conditional variable and $number –lt 11 is the conditional checking statement where it is checking whether the number is less than 11 where –lt is less than. If this condition satisfies then the body of the loop will execute where we are displaying number and incrementing the number so that during the next iteration it will display the next number and process repeats until it satisfies the condition.
4.5 (2,343 ratings)
2. For Loop
For loop is another type of looping statement to execute a set of commands for a certain number of times. Let’s have a look at the syntax of for loop in shell scripting and it can be represented as below:
for var in list
In the above for loop syntax, the list is a list of values or characters and var will have the values from the list one by one for each iteration and list of commands in the body of the loop will execute. Once the entire list is done then it will come out of the loop. In the above for loop syntax, for, do, and done are built-in keywords. Now we will understand for loop with an example in detail.
For loop, example to display names from a list.
for p_name in Stan Kyle Cartman
Code Explanation: In the above example, we are trying to iterative over the list of names and execute the commands inside the body of the loop until all the elements or values in the list are processed. In the above example, p_name is variable which will have the names for every iteration one by one and it will execute the echo command in the body of the for loop and the same process repeats until all the variables in the list are completed then it will come out of the loop.
3. Until Loop
Until loop is one of the looping statements in the shell scripting and this looping statement is similar to the while loop statement which we have discussed earlier. The difference between two is, it will execute the body of the loop until the conditional statement becomes true whereas while loop executes commands if the condition is true. Let us have a look at the syntax of until loop in the shell scripting as below:
until [ conditional statement ]
In the above until loop syntax, until, do, and done are built-in keywords of the shell scripting. This loop will do the same task as other loops are doing and it is almost similar to the while loop but in such situations, we need to use only until loop as it will be a better way to do that task rather than doing with while loop. Let us say leave the clothes on the stand until they are dry or leave the clothes on the stand while they are dry. Both sentences are correct but the first sentence seems to be more appropriate like that we need to choose until than while loop in a few situations.
Until example to display numbers from 1 to 10.
number = 1
until [ $number –gt 10 ] do
Code Explanation: In the above example, the number is a variable which we used to validate the condition and -gt is greater than some number. Until the loop will execute the body of the loop until the condition becomes true. In the above example, the body of the loop will display the numbers until the number is greater than 10 and the number is incremented for every iteration until the loop continues to execute. Once the condition evaluates to true then until loop will terminate. If we use the same condition in a while loop it will execute the body of the loop after the number becomes 11 and it won’t execute the loop when the number is from 1 to 10.
Finally, it’s an overview of loops in shell scripting. We have discussed what is a loop? Why do we need to use it? Different types of loops in shell scripting, what is while loop? What is for the loop? What is until loop? In shell scripting. I hope you will have a better understanding of the loops in shell scripting after reading this article.
This is a guide to Loops in Shell Scripting. Here we discuss what is a loop? why do we need to use it? and different types of loops in shell scripting along with examples and code implementation. You can also go through our other suggested articles to learn more –