Creating And Using An Associative Array In A Bash Script

Linux School Tech
3 min readFeb 4, 2024
Generated By: Adobe Firefly AI

An associative array in Bash is a data structure for storing key-value pairs. Each key is unique and has an associated value. Associative arrays provide a way to index and retrieve values based on corresponding keys. They first appeared in Bash version 4.

Here are some key points about associative arrays in Bash:

  • Key-Value Pairs: An associative array stores data in key-value pairs where every key is unique and has an associated value.
  • Indexing and Retrieving: Associative arrays provide a way to index and retrieve values based on corresponding keys. This makes it easier to manage and organize data.
  • Usage: Learning to use associative arrays can improve your Bash scripting skills, especially in complex data organization and parsing.
  • Version Compatibility: Associative arrays are available in Bash version 4 and later. To check the Bash version on your system, use the following command: echo $BASH_VERSION.
  • Syntax: The syntax for associative arrays differs between different Linux shells. For instance, in Bash, you can declare an associative array using the declare keyword with the -A option. To access a value in the array, you use the ${array_name[key]} syntax. To iterate over the keys in the array, you can use a for loop with the ${!array_name[@]} syntax.

Here is an example of how to declare and initialize an associative array in Bash:

# Declare an associative array
declare -A STAR_PLAYERS

# Add elements to the associative array
STAR_PLAYERS[Argentina]="Messi"
STAR_PLAYERS[Brazil]="Neymar"
STAR_PLAYERS[England]="Kane"

# Print the value of a specific key
echo ${STAR_PLAYERS[Argentina]} # Outputs: Messi

In this example, STAR_PLAYERS is an associative array. We declare it using declare -A, then we add elements to it by assigning values to specific keys. Lastly, we print the value of a specific key using the ${array_name[key]} syntax.

Assign New Value To Array

In Bash, you can add new values to an existing associative array using the assignment operator = or the += operator. Here's how you can do it:

Method 1

Using the Assignment Operator (=): This operator assigns a new value to a specific key in the array. If the key already exists in the array, this operation overwrites the existing value. Here's an example:

# Assume we have an associative array named example_array
declare -A example_array
example_array["key1"]="value1"

# Add a new key-value pair to the array
example_array["new_key"]="new_value"

In this example, we first declare an associative array named example_array and add a key-value pair to it. Then, we add a new key-value pair to the array using the assignment operator.

Method 2

Using the Append Operator (+=): This operator allows you to append a new key-value pair to an existing associative array. Like the assignment operator, if the key already exists in the array, this operation overwrites the existing value. Here's an example:

# Assume we have an associative array named example_array
declare -A example_array
example_array["key1"]="value1"

# Append a new key-value pair to the array
example_array+=([new_key]="new_value")

In this example, we use the += operator to append a new key-value pair to the example_array.

Remember that associative arrays are available in Bash version 4 and later.

Other Types Of Arrays

In Bash, there are two main types of arrays: indexed arrays and associative arrays.

Indexed Arrays

Indexed arrays are the simplest kind of array in Bash. They are essentially lists of items, where each item is accessed via its numerical index. Indexes start at zero. Here’s an example of declaring and initializing an indexed array:

indexed_array=("item1" "item2" "item3")

My YouTube Channel

More shell script videos and linux tutorials on my YouTube Channel.

--

--