The Bash “For” loop is a simple but powerful command to automate repetitive operations/tasks. It facilitates iterating over a series of elements, including numbers, files, etc. The Bash for loop provides a flexible way to customize the “For” loop according to your needs.
Through this educational guide, we will present the use of the Bash “For” loops, iterating over a range of numbers and working with files in a directory.
What is Bash for Loop With Examples?
The following examples effectively demonstrate the use of “For” loops in Bash scripts:
1. Simple For Loop in Bash Script
Here is the basic use of the “For” loop in Bash scripting.
Create a Bash file
First, create a new blank file for your Bash script with the “.sh” extension using the Nano editor:
nano for_loop.sh

The above command will open an empty file after creating it.
Write “for” loop script
To understand the work “For” loop in Bash, copy the following script to the “for_loop.sh” file:
#!/bin/bash
for color in Red Green Blue
do
echo "I like $color"
done

Explanation:
- In each iteration, the “for” loop searches each value in the list, such as Red, Green, and Blue, and stores the value in the “color” variable.
- The “do” and “done” is the start and end of the loop’s body. Inside the loop’s body, the command echo “I like $color” runs in each iteration and prints the list items.
Make the Bash file Executable
After saving the “for_loop.sh” file, you must make it executable. This command is necessary for executing the bash script in the terminal:
chmod +x for_loop.sh

Run the Bash file
Now, your bash file (i.e. for_loop.sh) is ready for execution from the terminal:
./for_loop.sh

As we are using the current directory, we used “./for_loop.sh” to run the script. If you are running the file from another path, you must specify the complete path of your Bash file, for example, /home/ubuntu/for_loop.sh.
2. Bash “For” Loop Over Numbers
With the following examples, you will learn how the for loop works over the range of numbers.
For Loop over Brace expansion
It is easy to iterate over various numbers using brace expansion “{}” in Bash scripting. For example:
#!/bin/bash
for number in {1..5}
do
echo "Serial No.: $number"
done

Explanation:
- In this example, {1..5} generates a sequence of numbers from 1 to 5.
- The commands within the loop’s body are executed for each value in the sequence and print the series 1, 2, 3, 4, 5.
Run the Bash file
Utilize the following command to run the file from your terminal:
./for_loop.sh

As mentioned above, the for loop executed and printed a range of numbers from 1 to 5 on your terminal.
For loop using increment expression
The following “for” loop script contains three parts: the initialization (i=1), condition (i<=5), and increment (i++) expressions. This method is used to iterate over a series of numbers (i.e. 1-5) in a more controlled way:
#!/bin/bash
for (( i=1; i<=5; i++ ))
do
echo "Iteration: $i"
done

Explanation:
- The “for” loop looks for the initial value, such as 1.
- Then it checks the condition, such as “i<=5”. If the condition is true, for example, “i” is less than 5, the command executes and prints “Iteration: 1”.
- The increment expression “i++” increases the value by 1 in each iteration and continues until the condition becomes false.
Run the Bash file
Use the below command to execute the bash file from your terminal:
./for_loop.sh

The for loop checks for the True condition, it executes the loop’s body, which is echo “Iteration: $i” and prints “Iteration: 1 to Iteration: 5”.
3. Bash “For” Loop Over Directory
The example below will explain the use of the “For” loop over the files in a directory.
Let’s confirm the content of the “for_dir” directory using the “ls” command:
ls ~/for_dir

Within the “for_dir” folder, we have five files with different file extensions, such as txt, doc, csv, jpg, and png.
Now, write a simple bash script to understand the working of the “for” loop over the files in a directory:
#!/bin/bash
dir=/home/ubuntu/for_dir
for files in "$dir"/*
do
echo "$files"
done

Explanation:
- First, define a “dir” variable and assign the directory path (i.e., /home/ubuntu/for_dir).
- Use the “for” loop to iterate over each file in the directory.
- Use the “$dir”/* to print all files in the directory.
Run the Bash file
Execute the bash file (i.e. ./for_loop.sh) from your terminal:
./for_loop.sh

It can be seen above that when the Bash script executes, it prints all files from the directory (i.e. for_dir) to the terminal, in a sequence.
FAQs
Why should I use For loop in Bash scripting?
In Bash, the “For” loop makes repetitive tasks easier. You can iterate over a sequence of elements, including reading files in a directory. Thus, to avoid repetitive tasks in Bash scripting, utilize the “For” loop.
Can I read files in a directory using the Bash “For” loop?
Yes, the Bash “For” loop allows you to read and iterate over files in a directory.
What is the file extension of a Bash script?
The file extension of a Bash script is “.sh”.
Conclusion
The Bash scripting provides features to iterate over a series of elements and display results. The Bash “For” loop facilitates you to iterate over numbers and files in directories efficiently. You can modify the “For” loop and the loop’s body to perform other operations according to your needs.