The for loop is a powerful and commonly used construct in Bash scripting. It allows you to iterate over a list of items and execute commands for each item in the list.
for variable in list
do
# Commands to execute for each item
donevariable: A placeholder that takes the value of each item in the list during each iteration.list: A sequence of items (numbers, strings, etc.) to iterate through.do ... done: The block of code that executes for each item in the list.
#!/bin/bash
for fruit in apple banana cherry
do
echo "I like $fruit"
doneOutput:
I like apple
I like banana
I like cherry
You can use brace expansion {start..end} to specify a range.
#!/bin/bash
for number in {1..5}
do
echo "Number: $number"
doneOutput:
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
You can also define a step value:
for number in {1..10..2}
do
echo "Odd number: $number"
doneOutput:
Odd number: 1
Odd number: 3
Odd number: 5
Odd number: 7
Odd number: 9
You can loop through the output of a command using command substitution.
#!/bin/bash
for file in $(ls *.txt)
do
echo "Processing $file"
doneOutput (example):
Processing file1.txt
Processing file2.txt
Processing file3.txt
Bash also supports C-style for loops.
#!/bin/bash
for ((i=1; i<=5; i++))
do
echo "Count: $i"
doneOutput:
Count: 1
Count: 2
Count: 3
Count: 4
Count: 5
You can use a wildcard to iterate over files.
#!/bin/bash
for file in /path/to/directory/*
do
echo "File: $file"
doneOutput (example):
File: /path/to/directory/file1
File: /path/to/directory/file2
File: /path/to/directory/file3
In standard for loops iterating over a list, the counter is the variable used in the loop:
for item in {A..C}
do
echo "Item: $item"
doneFor a C-style loop:
for ((i=0; i<3; i++))
do
echo "Counter: $i"
donebreak: Exit the loop entirely.continue: Skip to the next iteration.
Example with break:
for number in {1..10}
do
if [ $number -eq 5 ]; then
echo "Breaking the loop at $number"
break
fi
echo "Number: $number"
doneExample with continue:
for number in {1..5}
do
if [ $number -eq 3 ]; then
echo "Skipping $number"
continue
fi
echo "Number: $number"
donefor file in *.log
do
echo "Compressing $file"
gzip "$file"
donefor file in *.txt
do
mv "$file" "${file%.txt}.bak"
donefor service in nginx apache2 mysql
do
systemctl is-active --quiet $service && echo "$service is running" || echo "$service is stopped"
done-
Quote Variables: Use quotes to handle spaces or special characters in filenames or strings.
for file in *.txt do echo "Processing \"$file\"" done
-
Use
globbingcarefully: Globbing like*can match unintended files if not handled properly. -
Use Arrays for Complex Lists:
fruits=("apple" "banana" "cherry") for fruit in "${fruits[@]}" do echo "I like $fruit" done
-
Validate Input: When looping over files or command outputs, ensure the inputs are sanitized to avoid unexpected behavior.
-
Looping over command output with spaces: Use a
whileloop withreadfor better handling of spaces.ls | while read -r file do echo "File: $file" done
-
Overwriting Variables: Be cautious if the loop variable conflicts with other variables in the script.