Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Bash Syntax

Bash associative arrays

Reference for Bash associative arrays: declare with declare -A, set and read keys, list keys and values, check membership, delete entries and sort the output.

An associative array maps string keys to string values, like a dictionary or hash map. Unlike indexed arrays, you must declare one explicitly with declare -A (or local -A, typeset -A) before use; otherwise Bash treats the subscript as an arithmetic index.

They arrived in Bash 4.0. Key order is not defined: iterating gives keys in an internal order that can differ between runs and versions, so sort when you need stable output.

Terminal
declare -A age
age[ann]=31
age["bob smith"]=25
echo "${age[ann]}"

Try it

Bash Declare, set and read

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Counting words and awkward keys

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
declare -A name
name=([key1]=value1 [key2]=value2)
name[key]=value
name+=([key]=value)
${name[key]}  ${name[@]}  ${!name[@]}  ${#name[@]}
unset 'name[key]'
Expression Description
declare -A m Create an empty associative array (local inside functions; use declare -gA for global).
m[key]=v Set or replace the value of key.
${m[key]} Value of key, or empty if absent.
"${m[@]}" All values (order unspecified).
"${!m[@]}" All keys (order unspecified).
${#m[@]} Number of entries.
[[ -v m[key] ]] True if key exists, even with an empty value (Bash 4.3+).
unset 'm[key]' Remove one entry.

Usage notes

  • Forgetting declare -A is the most common error: Bash then makes an indexed array and evaluates the key as arithmetic, giving surprising results (plain[abc] becomes index 0).
  • Inside a function, declare -A creates a local array. Add -g to make it global.
  • Keys and values are strings. There are no nested arrays or values that are arrays. An empty string is not accepted as a key (bad array subscript).
  • Quote keys with spaces or glob characters: m["a b"]. Use ${m[$key]} with a variable key; quoting the variable ("$key") is also fine inside the brackets.
  • Iteration order is arbitrary. Sort the keys (sort) before printing or comparing results.
  • (( freq[$w]++ )) works even if the key is new, because an unset value counts as 0.
  • Test existence with -v, not with -n "${m[key]}", which also fails for empty values. For Bash 4.0โ€“4.2 use [[ -n ${m[key]+x} ]].
  • macOS ships Bash 3.2, which lacks associative arrays. On Linux, Bash 4+ is standard.
  • Assigning m=([a]=1) replaces the whole content; use += to add entries.