For loop in bash

0/5 No votes

Report this app

Description

Loop is a fundamental concept of any programming language and it is a very useful thing while doing some repetitive task. You can use it to execute a line or block of code repeatedly until a particular situation reached.

Basically, there are three types of loops used in bash scripting i.e. while loop, until loop, and for a loop. In this article, we will explain for loop used in bash along with some examples.

Bash for loop

The bash for loop is used to iterate over a list of items and perform a certain task by executing commands. You can use it to iterate between a range of numbers or on array elements.

The basic syntax of bash for loop is given below.

for item in <list>
do
  <commands>
done

Where instead of the list you can use a range of numbers or arrays.

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

Example 1: Loop on strings

In this example, we will use for loop to iterate over the items of a list that contains strings.

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

This will produce the given output.

Example 2: Loop over a range of number

To iterate over a range of numbers you need to pass the start and end point of the range within curly braces.

The following example shows how to iterate over a range of numbers using for loop.

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

You can see the output of this code in the given image.

From bash 4, you can specify the value to increase in every iteration of for loop.

For example –

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

This value ofi will be increased by 2 in every iteration. you can see this in the given image.

Example 3: Iterate on array elements

You can use bash for loop to iterate over the elements of an array.

For example –

PHONES=('samsung' 'oppo' 'vivo' 'mi' 'lava' 'iphone')
for phone in "${PHONES[@]}"; do
  echo "Phone name: $phone"
done

Here we iterate over the elements of the PHONE array and printing their name.

C style syntax of bash for loop

The c- style syntax of bash for loop is given below.

for ((initialization; test-expr; step))
do
   <commands>
done

When it is executed first a variable gets initialized with the given value. Next test-expr will be evaluated if it true the for loop executed and once all the statement executed under for loop the initialized value will be increased or decreased based on what is given. When the condition becomes false the control will exit from the loop.

For example –

The above code to print numbers from 1 to 10 can be written as.

# c stype for loop

for ((i = 0 ; i <= 10 ; i++)); do
  echo "$i"
done

Nested bash for loop

Nesting means using one for loop inside another one. The following example shows the nesting of for loop in bash.

# Nested for loop in bash

for i in 1 2 3 4 5  do
    for j in $(seq 1 $i)
    do
        echo  -n "*"
    done
    echo
done
echo

This code will print the following asterisk pattern.

Conclusion

You have learned bash for loop and its usage in this article. Now if you have a query then write us in the comments below.

Leave a Reply

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