How do you use associative arrays in shell scripting and when are they useful?
Learn from the community’s knowledge. Experts are adding insights into this AI-powered collaborative article, and you could too.
This is a new type of article that we started with the help of AI, and experts are taking it forward by sharing their thoughts directly into each section.
If you’d like to contribute, request an invite by liking or reacting to this article. Learn more
— The LinkedIn Team
Associative arrays are a powerful feature of shell scripting that allow you to store and manipulate key-value pairs of data. Unlike regular arrays, which use numeric indexes, associative arrays use strings as keys, making them more flexible and expressive. In this article, you will learn how to use associative arrays in shell scripting and when they are useful for various tasks.
Declaring and assigning associative arrays
To use associative arrays in shell scripting, you need to declare them with the declare -A command, followed by the name of the array. For example, declare -A colors declares an associative array named colors. To assign values to the array, you can use the syntax array[key]=value, where key and value are strings. For example, colors[red]=#FF0000 assigns the hexadecimal code for red to the key red in the colors array. You can also assign multiple values at once using the syntax array=([key1]=value1 [key2]=value2 ...). For example, colors=([red]=#FF0000 [green]=#00FF00 [blue]=#0000FF) assigns three values to the colors array.
Accessing and modifying associative arrays
To access the value of a key in an associative array, you can use the syntax ${array[key]}. For example, ${colors[red]} returns #FF0000. To access all the keys in an associative array, you can use the syntax ${!array[@]} or ${!array[*]}. For example, ${!colors[@]} returns red green blue. To access all the values in an associative array, you can use the syntax ${array[@]} or ${array[*]}. For example, ${colors[@]} returns #FF0000 #00FF00 #0000FF. To modify the value of a key in an associative array, you can use the same syntax as assigning a value, i.e. array[key]=value. For example, colors[red]=#FF0000 modifies the value of red in the colors array. To delete a key and its value from an associative array, you can use the unset command, followed by the array name and the key. For example, unset colors[red] deletes the key red and its value from the colors array.
Looping over associative arrays
To loop over the keys and values of an associative array, you can use a for loop with the syntax for key in ${!array[@]}; do ... done. For example, for key in ${!colors[@]}; do echo "$key: ${colors[$key]}"; done prints the key and value of each element in the colors array. Alternatively, you can use a while loop with the read command and the <<< operator to read each key and value from the array. For example, while read -r key value; do echo "$key: $value"; done <<< "${!colors[@]} ${colors[@]}" also prints the key and value of each element in the colors array.
Comparing and sorting associative arrays
To compare two associative arrays, you can use the == or != operators with the syntax ${array1[@]} == ${array2[@]} or ${array1[@]} != ${array2[@]}. For example, declare -A fruits; fruits=([apple]=red [banana]=yellow [orange]=orange); ${colors[@]} == ${fruits[@]} returns false, while ${colors[@]} != ${fruits[@]} returns true. To sort an associative array by its keys or values, you can use the sort command with the -t and -k options to specify the delimiter and the field to sort by. For example, printf "%s\n" "${!colors[@]}" | sort -t = -k 1 sorts the keys of the colors array in alphabetical order. Similarly, printf "%s\n" "${!colors[@]} ${colors[@]}" | sort -t = -k 2 sorts the values of the colors array in hexadecimal order.
Using arithmetic operations with associative arrays
To use arithmetic operations with associative arrays, you need to use the $(( )) or let command to perform arithmetic expansion on the values of the array. For example, declare -A numbers; numbers=([one]=1 [two]=2 [three]=3); echo $(( ${numbers[one]} + ${numbers[two]} )) prints 3, while let result=${numbers[three]}*${numbers[two]}; echo $result prints 6. You can also use the +=, -=, *=, /=, %=, and **= operators to modify the values of the array with arithmetic operations. For example, numbers[one]+=2 adds 2 to the value of one in the numbers array, while numbers[two]**=3 raises the value of two to the power of 3 in the numbers array.