Skip to content
Justice Tech School

Associative Arrays

declare -A creates a map from keys to values. ${!map[@]} lists keys and ${map[key]} reads one.

Example
declare -A age=([Ada]=21 [Tunde]=30)
age[Zed]=25
echo "${age[Ada]}"
for k in $(printf "%s\n" "${!age[@]}" | sort); do echo "$k=${age[$k]}"; done
echo "${#age[@]}"

Result

21
Ada=21
Tunde=30
Zed=25
3

Exercise

Declare a map.

declare  m

Quiz Test

Which flag makes an associative array?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.