Bash 5 reference · Shell Builtins and Syntax
for (Bash builtin)
Loop over words, files, ranges or array elements, or use the C-style form for counters.
Bash 5
Syntax
for NAME in WORDS; do ...; done | for (( init; cond; step )); do ...; done
Parameters
| Parameter | Description |
|---|---|
{1..5} |
Brace expansion range. |
"${arr[@]}" |
Array elements, each kept intact. |
*.log |
Files matching a glob. |
Examples
for env in dev prod; do echo "deploy $env"; done
for i in {1..3}; do printf '%s ' "$i"; done; echo
Output
deploy dev deploy prod 1 2 3
Run this on your computer — see “Get Started” for setup.
for (( i = 10; i > 0; i -= 4 )); do echo "$i"; done
Output
10 6 2
Run this on your computer — see “Get Started” for setup.
Related
while· Repeat commands while a condition succeeds; with read, it's the standard way to process input line by line.arrays· Indexed and associative arrays: create, append, read elements, count, slice and loop over them safely.- Bash Loops · tutorial