Iterating Arrays
In this page:
Iterating Just the Values
Looping directly over "${arr[@]}" is the simplest option when you only need each value and don't care about its position in the array. This works identically for both dense and sparse arrays.
Example: Iterating Just the Values
#!/bin/bash
tasks=("write code" "run tests" "deploy")
for task in "${tasks[@]}"; do
echo "Task: $task"
done
Login to try C/C++/Java/PHP code in the editor
Iterating Indices for Sparse Arrays
"${!arr[@]}" returns only the indices that actually have a value assigned, which correctly handles sparse arrays where some numeric positions were skipped. A plain for (( i=0; i<len; i++ )) loop would not know to skip missing indices.
Example: Iterating Indices for Sparse Arrays
#!/bin/bash
declare -a sparse
sparse[0]="first"
sparse[3]="fourth"
for idx in "${!sparse[@]}"; do
echo "Index $idx has value: ${sparse[idx]}"
done
Login to try C/C++/Java/PHP code in the editor
Iterating Index and Value Together
Looping over "${!arr[@]}" and then looking up ${arr[idx]} inside the loop body gives you both the position and the value on each pass. This pattern works correctly regardless of whether the array is dense or sparse.
Example: Iterating Index and Value Together
#!/bin/bash
players=(Alice Bob Carol)
for i in "${!players[@]}"; do
echo "Player #$i: ${players[i]}"
done
Login to try C/C++/Java/PHP code in the editor
Iterating an Associative Array by Key
For associative arrays, iterating over "${!arr[@]}" gives you the string keys (since there's no meaningful numeric position), and you look up each value with ${arr[key]}. This is the only correct general-purpose way to walk an associative array's contents.
Example: Iterating an Associative Array by Key
#!/bin/bash
declare -A prices
prices["coffee"]=4
prices["tea"]=3
for item in "${!prices[@]}"; do
echo "$item costs \$${prices[$item]}"
done
Login to try C/C++/Java/PHP code in the editor
- Forgetting the
!prefix means 'give me the indices', so${!arr[@]}and${arr[@]}look similar but return completely different things (positions vs. values). - Assuming a
forloop over${!arr[@]}will always produce a contiguous range like 0,1,2...; if the array is sparse, only the indices that actually exist are returned. - Using a C-style
for (( i=0; i<${#arr[@]}; i++ ))loop on a sparse array and assuming everyicorresponds to a real element; some of those indices might not exist, silently producing empty values.
for val in "${arr[@]}"iterates values directly, in index order.for idx in "${!arr[@]}"iterates the actual existing indices/keys, correctly skipping gaps in sparse arrays.- Combine the two (loop over indices, then look up
${arr[idx]}) whenever you need position and value together. - For associative arrays, only the
${!arr[@]}(keys) and lookup-by-key approach works -- there's no meaningful 'numeric position' to iterate by.
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: