If you are new to Bash scripting or Linux shell scripting, one of the most useful concepts you need to learn is the Bash for loop. A for loop allows you to run a command multiple times without writing it repeatedly. This makes tasks like file processing, renaming, or printing numbers much easier and faster.
In this guide, we will cover all the important types of Bash for loops with simple explanations and practical examples.
What is a Bash For Loop?
A for loop in Bash is a way to repeat commands for each item in a list. Instead of typing the same command over and over, the loop handles it for you.
Basic Syntax of a For Loop in Bash
for item in list
do
command to run
done
item
→ a variable that changes with each step of the looplist
→ the values the loop goes through (words, numbers, files, or array items)
Bash For Loop with Words (Beginner Example)
for fruit in apple banana cherry
do
echo "I like $fruit"
done
Output:
I like apple
I like banana
I like cherry
This loop goes through each word and prints it.
Loop Through Numbers
The most common use of a for loop in Bash is to work with numbers.
for i in {1..5}
do
echo "Number $i"
done
Output:
Number 1
Number 2
Number 3
Number 4
Number 5
Loop With Step Values
You can also skip numbers by using steps.
for i in {0..10..2}
do
echo "Even number: $i"
done
Output:
Even number: 0
Even number: 2
Even number: 4
Even number: 6
Even number: 8
Even number: 10
Bash For Loop to Process Files
A very useful case in Linux shell scripting is looping through files.
for file in *.txt
do
echo "Found file: $file"
done
Output (if files exist):
Found file: notes.txt
Found file: data.txt
Found file: report.txt
Loop Through Array Items
colors=("red" "green" "blue")
for c in "${colors[@]}"
do
echo "Color: $c"
done
Output:
Color: red
Color: green
Color: blue
C-Style Bash For Loop
If you have experience with languages like C or Java, this style will look familiar.
for ((i=1; i<=5; i++))
do
echo "Count: $i"
done
Output:
Count: 1
Count: 2
Count: 3
Count: 4
Count: 5
Using Break and Continue
You can control loops using break
and continue
.
for i in {1..5}
do
if [ $i -eq 3 ]; then
continue
fi
if [ $i -eq 5 ]; then
break
fi
echo "Value: $i"
done
Output:
Value: 1
Value: 2
Value: 4
Explanation:
- Skips number 3 because of
continue
- Stops the loop completely at 5 because of
break
Nested For Loops Example
You can put one loop inside another to combine values.
for i in 1 2
do
for j in A B
do
echo "$i$j"
done
done
Output:
1A
1B
2A
2B
Real-Life Example: Rename Files
for file in *.jpg
do
mv "$file" "backup_$file"
done
If you have files like image1.jpg
and image2.jpg
, after running the loop they become:
backup_image1.jpg
backup_image2.jpg
Conclusion:
Learning this concept is essential for anyone who wants to get better at Linux shell scripting.
- It saves time by automating repetitive tasks.
- It works with numbers, words, arrays, and files.
- It can be controlled with
break
andcontinue
. - It is powerful enough for real-life tasks like file processing and automation.
Mastering Bash scripting for beginners often starts with understanding loops. Once you are comfortable with for loops, you can easily move on to more advanced Bash scripting concepts.