Examples of associative arrays in Bash.
First, declare the variable as an associative array:
$ declare -A aa
Assign some data:
$ aa["one"]=1 $ aa["two"]=2
Determine how many elements are in the array:
$ echo ${#aa[*]} 2
Get the keys:
$ echo ${!aa[@]} one two
Note the difference between the @ and * subscripts is subtle. * expands the array with the IFS character between each word. @ expands to just words.
To add another item and see what the actual array looks like enter:
$ aa["three"]=3 $ typeset -A declare -A aa='([one]="1" [two]="2" [three]="3" )'
To remove elements use unset:
$ unset aa["three"] $ typeset -A | grep aa= declare -A aa='([one]="1" [two]="2" )'