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

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

for NAME in WORDS; do ...; done   |   for (( init; cond; step )); do ...; done

Parameters

ParameterDescription
{1..5} Brace expansion range.
"${arr[@]}" Array elements, each kept intact.
*.log Files matching a glob.

Examples

Words and ranges
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.

C-style loop
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.

Was this page helpful?