Shell – Associative Arrays in Shell Scripts

associative arrayshell-script

I saw a trick for implementing associative arrays in a shell script. For example print array["apples"] could be scripted as echo \$array$key where key=apples.

However, there was no mention of how to generate the keys to iterate over the array.
The only way I could think of was to store the keys in a variable delimited by spaces so I could use a for-loop to iterate over the array.

So, is there some other way to store the keys for later use?

Best Answer

Shells with associative arrays

Some modern shells provide associative arrays: ksh93, bash ≥4, zsh. In ksh93 and bash, if a is an associative array, then "${!a[@]}" is the array of its keys:

for k in "${!a[@]}"; do
  echo "$k -> ${a[$k]}"
done

In zsh, that syntax only works in ksh emulation mode. Otherwise you have to use zsh's native syntax:

for k in "${(@k)a}"; do
  echo "$k -> $a[$k]"
done

${(k)a} also works if a does not have an empty key.

In zsh, you could also loop on both keys and values at the same time:

for k v ("${(@kv)a}") echo "$k -> $v"

Shells without associative arrays

Emulating associative arrays in shells that don't have them is a lot more work. If you need associative arrays, it's probably time to bring in a bigger tool, such as ksh93 or Perl.

If you do need associative arrays in a mere POSIX shell, here's a way to simulate them, when keys are restricted to contain only the characters 0-9A-Z_a-z (ASCII digits, letters and underscore). Under this assumption, keys can be used as part of variable names. The functions below act on an array identified by a naming prefix, the “stem”, which must not contain two consecutive underscores.

## ainit STEM
## Declare an empty associative array named STEM.
ainit () {
  eval "__aa__${1}=' '"
}
## akeys STEM
## List the keys in the associatve array named STEM.
akeys () {
  eval "echo \"\$__aa__${1}\""
}
## aget STEM KEY VAR
## Set VAR to the value of KEY in the associative array named STEM.
## If KEY is not present, unset VAR.
aget () {
  eval "unset $3
        case \$__aa__${1} in
          *\" $2 \"*) $3=\$__aa__${1}__$2;;
        esac"
}
## aset STEM KEY VALUE
## Set KEY to VALUE in the associative array named STEM.
aset () {
  eval "__aa__${1}__${2}=\$3
        case \$__aa__${1} in
          *\" $2 \"*) :;;
          *) __aa__${1}=\"\${__aa__${1}}$2 \";;
        esac"
}
## aunset STEM KEY
## Remove KEY from the associative array named STEM.
aunset () {
  eval "unset __aa__${1}__${2}
        case \$__aa__${1} in
          *\" $2 \"*) __aa__${1}=\"\${__aa__${1}%%* $2 } \${__aa__${1}#* $2 }\";;
        esac"
}

(Warning, untested code. Error detection for syntactically invalid stems and keys is not provided.)

Related Question