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:
continue
break
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
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.
How Does a Firewall Work Step by Step? What Is a Firewall and How Does…
ROADTools is a powerful framework designed for exploring and interacting with Microsoft Azure Active Directory…
Microsoft 365 Groups (also known as M365 Groups or Unified Groups) are at the heart…
SeamlessPass is a specialized tool designed to leverage on-premises Active Directory Kerberos tickets to obtain…
PPLBlade is a powerful Protected Process Dumper designed to capture memory from target processes, hide…
HikPwn: Comprehensive Guide to Scanning Hikvision Devices for Vulnerabilities If you’re searching for an efficient…