Loops in bash shell scripting

0/5 No votes

Report this app

Description

A loop in bash is used to execute a line or block of code repeatedly until a particular situation reached. Loop is a fundamental concept of programming and a very useful thing while doing some repetitive task.

For example, You can print numbers from 1 to 10 by using a print statement 10 times printing numbers from 1 to 10, or use a loop with a condition to run it 10 times and print the numbers.

In this article, we will discuss the different types of loops used in bash scripting along with some examples.

Types of loops in bash

Bash has three basic loop constructs these are given below.

  • while loop
  • for loop
  • until loop

The bash select is another loop construct used especially to create a menu of numbered items, a user can select an item from the created menu.

Bash while loop

It is one of the easiest and most basic types of loop in bash. It executes a line or block of code repeatedly until a certain condition is satisfied.

The syntax of the of while loop in bash is given below.

while [ test-expression ]
do
<commands>
done

The given example shows the use of bash while loop.

#!/bin/bash

#Initialization of variable
count=1
# Check if count is less than or equal to 10
while [ $count -le 10 ]
do
#Print the value of count
  echo $count
# Increment the value of count
  ((count++))
done

Let’s say this code is saved in the ex1.sh file. Use the following command to execute it.

bash ex1.sh

This will print the numbers from 1 to 10.

Bash for loop

This is another type of loop that is used to iterate over a list of items and perform a certain task by executing commands.

The bash for loop syntax is given below.

for item in <list>
do
  <commands>
done

The following examples show the usage of for loop in bash scripting.

Example 1:

for fruit in Orange Apple Banana Grapes Blueberries
do
   echo "Fruit name: $fruit"
done

You will see the given output on the execution of this code.

Example 2:

To iterate over a range of numbers you need to pass the range. You can see this in the given example.

for i in {1..10}
do
   echo "$i"
done

When you execute this command. The numbers from 1 to 10 will get printed. You can see the output in the given image.

Bash until loop

The until loop is very similar to the while loop which is explained above. The difference is that it will execute the commands within it until the test becomes true.

The syntax of bash until loop is given below.

until [ test-expression ]
do
   <commands>
done

The following example shows the use of until loop in bash.

#!/bin/bash
# Intialization of counter variable
count=1
# Condition check
until [ $count -gt 10 ]
do
# Print the value of count variable
   echo $count
#Increment the counter variable
   ((count++))
done

On execution of this script, you will see the given output.

You can see the difference between while and until loops while checking the condition.

Bash select

The select construct is used in bash scripting it provides a prompt and a menu of numbered items, a user can select from these options. If the user enters a valid option it executes the code written for that option. If the user presses enter without choosing an option then it will display the menu again. The select command is generally used with the case command.

The syntax of bash select is given below.

select item in <list>
do
   <commands>
done

For example –

#! /bin/bash
# Author : Lalit Kumar
# Copyright(c) : cyanogenmods.org

PS3="Enter a number: "
select phone in samsung mi oppo vivo iphone lava asus
do
   echo "You selected: $phone"
   echo "And you selected the number: $REPLY"
done

On executing this code you will see the given output.

Conclusion

I hope now you understand how to use different loops in bash. If you have any query then write us in the comments below.

Leave a Reply

Your email address will not be published. Required fields are marked *