Bash

Bash For Loop

Bash For Loop

In this topic, we'll understand the usage of for loop in Bash scripts.

Like any other programing language, bash shell scripting also supports 'for loops' to perform repetitive tasks. It helps us to iterate a specific set of statements over a series of words during a string, or elements in an array. for instance, you'll either run the UNIX command (or task) repeatedly or simply read and process the list of commands employing a 'for loop'.

Syntax of For Loop

We can apply 'for loop' on the bash script in two ways. a method is 'for-in' and differently is that the c-style syntax. Following is that the syntax of 'for loop' in bash shell scripting:

for variable in list 

do 

commands 

done 

Or

for (( expression1; expression2; expression3 )) 

do 

commands 

done 

There are some key points of the 'for loop' statement:

Each block of 'for loop' in bash starts with the 'do' keyword followed by the commands inside the block. The 'for loop' statement is closed by the 'done' keyword.

The number of your time that a 'for loop' will iterate depends on the declared list variables.

The loop will select one item from the list and assign the worth on a variable that can be used within the loop.

After the execution of commands between 'do' and 'done', the loop goes back to the highest and choose a subsequent item from the list and repeat the entire process.

The list can contain numbers or string etc. separated by spaces.

Some of the 'for loop' examples are given below for instance how do they work:

Example1:

In the Following we will print each word of a string / sentence in a single line.

#!/bin/bash  
#This is the basic example of 'for loop'.  
  
learn="Start learning from Great Learning Academy and Achieve Your goals."  
  
for learn in $learn  
do  
echo $learn  
done  
  
echo "Thank You."

Output of the above code:
Text

Description automatically generated

For loop to print a range of numbers

Script:

#!/bin/bash  
#This is the basic example to print a series of numbers from 1 to 10.  
#This is a for lopp which will print all the value in a range
  
for num in {1..10}  
do  
echo $num  
done  
  
echo "Series of numbers from 1 to 10." 

Output:
Text

Description automatically generated

Example3:

The next code snippet will give a output of 1 to 100 with a increment of 10.

#!/bin/bash  
  
#For Loop to Read a Range with Increment of 10 
  
for num in {1..100..10}  
do  
echo $num  
done  

The Output of the above code ,
Text

Description automatically generated

Example 4 :  The next code will Decrement the range by a certain value.

Script:

#!/bin/bash  
  
#For Loop to Read a Range with Decrement  
  
for num in {100..0..10}  
do  
echo $num  
done  

Output of the above cod,
Text

Description automatically generated

For Loop to Read Array Variables

We can use 'for loop' to iterate the values of an array.

The syntax are often defined as:

array=( "element1" "element 2" . . "elementN" ) 
 for i in "${arr[@]}" 

do 

echo $i 

done 

Output

For every single element in an 'array', the statements or set of commands from 'do' till 'done' are performed. Each element might be accessed as 'i' within the loop for the respective iteration. inspect the instance below explaining the utilization of 'for loop' to iterate over elements of an array:

Script:

#!/bin/bash  
  
#Array Declaration  
arr=( "Welcome""to""Great""Learning""Academy" )  
  
for i in "${arr[@]}"  
do  
echo $i  
done  

Output of the above code.