for Loops in Bash
From the Bash Scripting cheat sheet · Loops · verified Jul 2026
for Loops
Iterate lists, ranges, and globs.
bash
# Quick Reference
for item in a b c; do echo "$item"; done
for i in {1..5}; do echo "$i"; done
for f in *.txt; do echo "$f"; done💡 Loop over globs (for f in *.txt) directly - never parse ls output.
⚡ {1..5} generates a range; {0..10..2} adds a step. Ranges cannot use variables.
📌 Guard glob loops with [[ -e "$f" ]] || continue in case nothing matches.
🟢 Quote "$@" in for arg in "$@" so arguments with spaces stay intact.
loopsfor