Using associative arrays

How to:

First off, declare an associative array in Bash:

declare -A my_array

Then, you can start populating it with values, using strings as keys:

my_array["name"]="Linux Journal"

To access an element, use its key:

echo ${my_array["name"]}  # Outputs: Linux Journal

Iterating over keys and values is also straightforward:

for key in "${!my_array[@]}"; do
    echo "$key: ${my_array[$key]}"

Sample output could look like this:

name: Linux Journal
topic: Programming

To add or modify elements, just assign a value to a key, similarly to the initial population:


And to remove an element, use unset:

unset my_array["topic"]

Deep Dive

Associative arrays were introduced in Bash version 4.0, making them a relatively recent addition to the language. Before their introduction, handling non-integer index arrays was cumbersome, often requiring workarounds or external tools like awk or sed.

Under the hood, Bash implements associative arrays using hash tables. This implementation allows for efficient key lookup, which remains fairly constant regardless of the array size, a critical feature for performance in script execution.

While associative arrays in Bash bring a lot of power and flexibility to shell scripting, they come with their own set of limitations, such as being somewhat clumsier to work with compared to arrays in higher-level languages like Python or JavaScript. For complex data manipulation tasks, it might still be worth considering external tools or languages better suited for the job.

However, for many typical scripting tasks, associative arrays provide a valuable tool in the Bash programmer’s toolkit, enabling more readable and maintainable scripts by allowing the use of meaningful string keys instead of numeric indexes.