Loops are used in any programming language to execute the same code repeatedly. Three types of loops are mainly used in programming for doing repetitive tasks. These are for, while and do-while/repeat-until loop. You can apply for loop on bash script in various ways. Some useful BASH for loops examples are mentioned in this article.
Syntax of for loop:
The starting and ending block of for loop are defined by do and done keywords in bash script. How many times a for loop will iterate depends on the declared lists variable. The loop will take one item from the lists and store the value on a variable which can be used within the loop. The use of different types of bash for loops example are explained below. Open a text editor to test the following code examples.
Example-1: Reading static values
Create a bash file named loop1.sh which contains the following script.
echo "Color = $color"
In this example, 5 static values are declared in the lists part. This loop will iterate 5 times and each time It will receive a value from the lists and store it in the variable named color which will print inside the loop. The following output will appear if you run loop1.sh.
Example-2: Reading Array Variable
You can use for loop to iterate the values of an array. Create a new bash file named loop2.sh with the following code.
for color in $ColorList
if [ $color == 'Pink' ]
echo "My favorite color is $color"
In this example, the loop retrieves the values from an array variable named ColorList and it will print the output only if Pink value is found in the array elements.
Example-3: Reading Command-line arguments
Command-line arguments values can be iterated by using for loop in bash. Create a new bash file named loop3.sh with the following code.
echo "Argument: $myval"
The following output will appear after running the script. Three arguments are given as command-line arguments in this example. These are ‘I’, ‘like’ and ‘Programming’
Example-4: Finding odd and even number using three expressions
The most common syntax of for loop is three expression syntax. First expression indicates initialization, second expression indicates termination condition and third expression indicates increment or decrement. Create a new file named loop4.sh to check the script.
if (( $n%2==0 ))
echo "$n is even"
echo "$n is odd"
The loop will iterate for 5 times from the value 1 to 5 and it will check and print even and odd numbers. You will get the following output after executing the script.
Example-5: Reading file content
You can use for loop to read content of any file by using ‘cat’ command. Suppose, you have a file named ‘weekday.txt’ which contains the name of all week days. Now, create a bash file named loop5.sh to read the content of the file.
for var in `cat weekday.txt`
echo "Weekday $i: $var"
The following output will appear after running the script.
By using for loop, in bash, you can traverse data from different sources and different ways and become more productive on the terminal or in your scripts. Check out the related video below: