Our example array:
arr=(a b c d e f)
Using a for..in
loop:
for i in "${arr[@]}"; do
echo "$i"
done
Using C-style for
loop:
for ((i=0;i<${#arr[@]};i++)); do
echo "${arr[$i]}"
done
Using while
loop:
i=0
while [ $i -lt ${#arr[@]} ]; do
echo "${arr[$i]}"
i=$((i + 1))
done
Using while
loop with numerical conditional:
i=0
while (( $i < ${#arr[@]} )); do
echo "${arr[$i]}"
((i++))
done
Using an until
loop:
i=0
until [ $i -ge ${#arr[@]} ]; do
echo "${arr[$i]}"
i=$((i + 1))
done
Using an until
loop with numerical conditional:
i=0
until (( $i >= ${#arr[@]} )); do
echo "${arr[$i]}"
((i++))
done