for
The Bash for loop iterates over lists, files, command output, and numeric ranges. In addition to the simple for variable in list form, you can also use the C-style for (( init; condition; update )) form.
Syntax
Iterate using a list form.
for variable in list; do
commands
done
Iterate using C style.
for (( init; condition; update )); do
commands
done
Combined with brace expansion.
for variable in {1..10}; do
commands
done
Combined with a file glob.
for file in *.txt; do
commands
done
Syntax Patterns
| Pattern | Description |
|---|---|
| for i in 1 2 3 | Iterates over a space-separated list in order. |
| for i in {1..10} | Iterates over integers from 1 to 10. |
| for i in {1..10..2} | Iterates in steps of 2: 1, 3, 5, ... |
| for f in *.php | Iterates over .php files in the current directory. |
| for f in $(cmd) | Iterates over the output of a command, line by line. |
| for (( i=0; i<n; i++ )) | C-style counter loop. |
| break | Exits the loop early. |
| continue | Skips the current iteration and moves to the next. |
Sample Code
Iterates over a space-separated list in order.
for_fruits.sh
#!/bin/bash
for fruit in apple banana cherry; do
echo "Fruit: $fruit"
done
bash for_fruits.sh
Fruit: apple
Fruit: banana
Fruit: cherry
You can also enter a for loop directly in the terminal. After pressing Enter following do, a > prompt appears — this means input is still expected. Enter done to execute.
for fruit in apple banana cherry; do
echo "Fruit: $fruit"
done
Fruit: apple
Fruit: banana
Fruit: cherry
Use brace expansion {1..9} to specify a numeric range.
multiply.sh
for i in {1..9}; do
echo "3 × $i = $(( 3 * i ))"
done
bash multiply.sh
3 × 1 = 3
3 × 2 = 6
3 × 3 = 9
...
3 × 9 = 27
Use a C-style counter loop to calculate the sum from 1 to 100.
sum_100.sh
sum=0
for (( i=1; i<=100; i++ )); do
(( sum += i ))
done
echo "Sum from 1 to 100: $sum"
bash sum_100.sh
Sum from 1 to 100: 5050
Use a file glob (wildcard) to process files in the current directory.
count_lines.sh
for f in *.php; do
if [ -f "$f" ]; then
echo " $f ($(wc -l < "$f") lines)"
fi
done
bash count_lines.sh
index.php (120 lines)
ajax.php (85 lines)
Use continue to skip even numbers, and break to stop the loop when 7 is reached.
skip_break.sh
for i in {1..10}; do
if (( i % 2 == 0 )); then
continue # skip even numbers
fi
if (( i == 7 )); then
break # stop at 7
fi
echo "$i"
done
bash skip_break.sh
1
3
5
Expand array elements with "${colors[@]}" and iterate over them.
colors.sh
colors=("red" "green" "blue" "yellow")
for color in "${colors[@]}"; do
echo "Color: $color"
done
bash colors.sh
Color: red
Color: green
Color: blue
Color: yellow
Notes
File globs ('*.php') can be used directly in a for loop, but using command substitution like 'for f in $(ls *.php)' can cause unexpected word splitting on filenames with spaces. Use globs directly for safety.
When processing a large number of files, piping find output to xargs is often more memory-efficient. For condition-based loops, use while / until.
If you find any errors or copyright issues, please contact us.