Associative Arrays
In this page:
Declaring an Associative Array
Associative arrays require an explicit declare -A arrname before assigning any key-value pairs, unlike indexed arrays which can be created implicitly. Once declared, values are set using string keys inside the brackets, arr["key"]=value.
Example: Declaring an Associative Array
#!/bin/bash
declare -A ages
ages["Ada"]=36
ages["Grace"]=85
ages["Linus"]=54
echo "Ada's age: ${ages["Ada"]}"
Login to try C/C++/Java/PHP code in the editor
Iterating Over Keys and Values
${!arr[@]} expands to all the keys currently in the associative array, and ${arr[key]} looks up the value for a specific key, so combining a loop over the keys with a lookup gives you every key-value pair. Order of iteration is not guaranteed to match insertion order.
Example: Iterating Over Keys and Values
#!/bin/bash
declare -A capitals
capitals["France"]="Paris"
capitals["Japan"]="Tokyo"
capitals["Egypt"]="Cairo"
for country in "${!capitals[@]}"; do
echo "$country -> ${capitals[$country]}"
done
Login to try C/C++/Java/PHP code in the editor
Checking If a Key Exists
[[ -v arr[key] ]] tests whether a given key exists in the associative array, regardless of whether its value is empty, which is different from checking whether the looked-up value is a non-empty string. This distinction matters because a key can legitimately be set to an empty string.
Example: Checking If a Key Exists
#!/bin/bash
declare -A settings
settings["debug"]=""
if [[ -v settings["debug"] ]]; then
echo "'debug' key exists (even though its value is empty)"
fi
if [[ ! -v settings["missing"] ]]; then
echo "'missing' key does not exist"
fi
Login to try C/C++/Java/PHP code in the editor
Counting Entries and Removing a Key
${#arr[@]} gives the number of key-value pairs in an associative array, just as it does for indexed arrays. unset arr[key] removes a specific key-value pair entirely.
Example: Counting Entries and Removing a Key
#!/bin/bash
declare -A stock
stock["apples"]=10
stock["bananas"]=5
echo "Entry count: ${#stock[@]}"
unset 'stock[bananas]'
echo "Entry count after removal: ${#stock[@]}"
Login to try C/C++/Java/PHP code in the editor
- Forgetting
declare -Ais mandatory before using an associative array; without it, Bash treatsarr[key]=valueas an indexed array where a non-numeric key is coerced/misinterpreted, causing errors. - Assuming associative arrays preserve insertion order when iterated; they do not guarantee any particular order for
${!arr[@]}or${arr[@]}, unlike indexed arrays. - Using unquoted keys that could be mistaken for glob patterns or contain spaces, causing lookups to fail or behave unexpectedly.
- Must be declared explicitly first with
declare -A arrname-- there is no implicit associative array creation. - Keys are arbitrary strings, e.g.
ages["Ada"]=36, rather than sequential integers. ${!arr[@]}gives all the keys;${arr[@]}gives all the values (order is not guaranteed).- Checking if a key exists is done with
[ -v arr[key] ](or[[ -v arr[key] ]]) or${arr[key]+_}, not by checking if the value is non-empty (a key could legitimately hold an empty string).
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: