1. Overview

A hash table is a fundamental data structure that allows storing data in the form of key-value pairs. Hash tables are natively supported in languages such as Perl and Python. Yet, sometimes they’re known under different names such as dictionaries. Modern versions of Bash also support hash tables in the form of associative arrays.

In this tutorial, we’ll explore how we can define a hash table in Bash and perform basic storage and retrieval operations.

2. Defining a Hash Table

We can define a hash table in Bash by declaring an associative array with the declare command:

$ declare -A languages=([en]=ENGLISH [es]=SPANISH [it]=ITALIAN)

The -A flag of declare defines an associative array. In this case, we name the array languages and assign it three key-value pairs. The keys are en, es, and it, whereas their respective values are ENGLISH, SPANISH, and ITALIAN.

Importantly, associative arrays are supported natively after Bash version 4.

3. Retrieving Keys

To access the keys of the associative array, we can use the ${!languages[@]} syntax:

$ for key in "${!languages[@]}"; do echo "$key"; done

Here, we access the keys in a for loop and print each key via echo.

Notably, the syntax is very similar to that for regular arrays but includes an ! exclamation point prefix to extract their keys.

4. Retrieving Values

Similarly, we can print the values of the associative array by directly accessing its elements:

$ for value in "${languages[@]}"; do echo "$value"; done

Notably, the values appear in the same order as the keys that were printed earlier.

We can also print the value for a specific key:

$ echo "${languages[es]}"

Here, we choose to print the value for the es key.

5. Retrieving Key-Value Pairs

We can also print the entire hash table by iterating over each key and printing both the key and its value:

$ for i in "${!languages[@]}"; do echo "${i}: ${languages[$i]}"; done

In this case, we use a colon followed by a space to separate each key from its value when printing.

In summary, we use the name of the associative array to extract the value based on the keys we iterate through.

6. Counting the Number of Elements

We can also extract the number of elements stored in the associative array using the ${#languages} syntax:

$ echo "${#languages[@]}"

Consequently, we see that there are three values stored in the array. This syntax matches the one for regular arrays as well.

7. Adding a Key-Value Pair

We can add a new key-value pair to the array via direct assignment:

$ languages[de]=GERMAN

Next, we can print the array to view the added pair:

$ for i in "${!languages[@]}"; do echo "${i}: ${languages[$i]}"; done

The new entry appears in the second row here.

8. Overwriting a Key-Value Pair

By assigning a new value to an existing key, we simply overwrite its value:

$ languages[es]=spanish
$ echo "${languages[es]}"

Here, we changed the value associated with the es key by converting it to lowercase.

9. Deleting a Key-Value Pair

To delete a key-value pair, we can use the unset command while specifying the key:

$ unset languages[de]

Next, we print the new array after removing the value we previously added:

$ for i in "${!languages[@]}"; do echo "${i}: ${languages[$i]}"; done
es: spanish

As a result, we see that the de key and its value disappear from the hash table.

To delete more than one key-value pair, we can iterate over the keys in a for loop:

$ for i in es it; do unset languages["$i"]; done

Subsequently, we print the hash table to view the changes:

$ for i in "${!languages[@]}"; do echo "${i}: ${languages[$i]}"; done

The associative array now consists of only one key-value pair.

10. Conclusion

In this article, we explored how to define a hash table in Bash using associative arrays. We also saw how to store and retrieve keys and their values, as well as how to print a hash table and delete any of its key-value pairs using unset.

Comments are closed on this article!