for
| Since: | All Linux | |
|---|---|---|
| macOS(2001 Cheetah) | ||
| Bash 1.0(1989) |
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.
sample_for_members.sh
#!/bin/bash
for member in "Yagami Iori" "Kusanagi Kyo" "Terry Bogard"; do
echo "Member: $member"
done
The following example demonstrates this:
bash for_members.sh Member: Yagami Iori Member: Kusanagi Kyo Member: Terry Bogard
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 member in "Yagami Iori" "Kusanagi Kyo" "Terry Bogard"; do
echo "Member: $member"
done
Member: Yagami Iori
Member: Kusanagi Kyo
Member: Terry Bogard
Use brace expansion {1..9} to specify a numeric range.
sample_multiply.sh
for i in {1..9}; do
echo "3 × $i = $(( 3 * i ))"
done
The following example demonstrates this:
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.
sample_sum_100.sh
sum=0
for (( i=1; i<=100; i++ )); do
(( sum += i ))
done
echo "Sum from 1 to 100: $sum"
Run the following command:
bash sum_100.sh Sum from 1 to 100: 5050
Use a file glob (wildcard) to process files in the current directory.
sample_count_lines.sh
for f in *.php; do
if [ -f "$f" ]; then
echo " $f ($(wc -l < "$f") lines)"
fi
done
Run the following command:
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.
sample_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
Run the following command:
bash skip_break.sh 1 3 5
Expand array elements with "${fighters[@]}" and iterate over them.
sample_fighters.sh
fighters=("Yagami Iori" "Kusanagi Kyo" "Terry Bogard" "Blue Mary")
for fighter in "${fighters[@]}"; do
echo "Fighter: $fighter"
done
Run the following command:
bash fighters.sh Fighter: Yagami Iori Fighter: Kusanagi Kyo Fighter: Terry Bogard Fighter: Blue Mary
Common Mistakes
Common Mistake 1: for in $(ls) splits on spaces in filenames
for f in $(ls) uses command substitution, which is subject to word splitting. Filenames with spaces are broken into multiple items.
for f in $(ls *.txt); do echo "$f"; done my file.txt ("my file.txt" was split)
Use a glob pattern directly to avoid word splitting.
for f in *.txt; do echo "$f"; done my file.txt
Common Mistake 2: Not quoting array elements causes spaces to split them
Expanding ${array[@]} without quotes splits elements at every space character.
fighters=("Yagami Iori" "Terry Bogard")
for f in ${fighters[@]}; do echo "$f"; done
Yagami
Iori
Terry
Bogard
Always quote the expansion to preserve elements that contain spaces.
for f in "${fighters[@]}"; do echo "$f"; done
Yagami Iori
Terry Bogard
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.