Bash: Loops
For loops
The generic form of a for loop is:
for ELEM in LIST
do
...
done
However, the in LIST part is optional. When it’s not present, then it’s assumed to be in "$@". So the following for loops are equivalent:
for ELEM
do
...
done
for ELEM in "$@"
do
...
done
While loops
A basic while loop for a countdown is:
i=3
while (( $i >= 0 ))
do
printf "$i\n"
i=$((i - 1))
done
3
2
1
0
and there is an until form that we can also use:
i=3
until (( $i == -1 ))
do
printf "$i\n"
i=$((i - 1))
done
3
2
1
0