For Loop
SH · Bash Scriptingsyntax
for var in list; do
commands
doneexample
for host in web01 web02 web03; do
echo "Deploying to ${host}..."
scp app.tar.gz deploy@"${host}":/opt/releases/
done
# C-style:
for ((i=1; i<=5; i++)); do
echo "Attempt ${i}"
doneoutput
Deploying to web01...
Deploying to web02...
Deploying to web03...Note Never parse ls output in a for loop. Use globs instead: for f in *.log; do ... done. For iterating over lines in a file: while IFS= read -r line; do ... done < file.txt.