Loops
for over words
Iterate a list. Quote expansions when items may contain spaces—often you loop over "$@" or lines carefully.
for f in *.txt; do
[[ -e "$f" ]] || continue
echo "File: $f"
done
C-style for
Useful for numeric ranges.
for ((i = 1; i <= 3; i++)); do
echo "$i"
done
while read
Read lines from a file or pipeline. Adjust IFS and -r to handle backslashes.
while IFS= read -r line; do
echo "Line: $line"
done < input.txt
until
Loops until a command succeeds—handy for waiting.
until ping -c1 example.com >/dev/null 2>&1; do
sleep 2
done
Comments
One comment per signed-in account. Comments are saved with this page’s URL.