Bash For Loop Examples
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.
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.
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)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.
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
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
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
colors=("red" "green" "blue")
for c in "${colors[@]}"
do
echo "Color: $c"
done
Output:
Color: red
Color: green
Color: blue
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
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:
continuebreakYou 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
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
Learning this concept is essential for anyone who wants to get better at Linux shell scripting.
break and continue.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.
General Working of a Web Application Firewall (WAF) A Web Application Firewall (WAF) acts as…
How to Send POST Requests Using curl in Linux If you work with APIs, servers,…
If you are a Linux user, you have probably seen commands like chmod 777 while…
Vim and Vi are among the most powerful text editors in the Linux world. They…
Working with compressed files is a common task for any Linux user. Whether you are…
In the digital era, an email address can reveal much more than just a contact…