BASH Programming

How to Use a Key Value Dictionary in Bash

Bash is a powerful tool that allows you to automate complex tasks and write custom scripts to process data. One of the most useful features of bash is the ability to use key-value dictionaries to store and manipulate data. Dictionaries allow you to store and access data quickly and easily, making your code more efficient. In this article, we will discuss how to use a key-value dictionary in bash.

What is a Key-Value Dictionary

A key-value dictionary is a data structure that allows you to store and access data by key. Each key has a corresponding value, which can be any type of data, such as a string, integer, or even another data structure and below is the syntax for adding values to keys followed by the syntax of creating a dictionary in bash:

declare -A <dict_name>

 

This creates an empty dictionary named dict_name with the -A flag indicating that it is an associative array. Once you have created a dictionary, you can add keys and values to it using the following syntax:

<dict_name>[<key>]=<value>

 

How to Use a Key-Value Dictionary in Bash

Using a key-value dictionary in bash comes handy in many scenarios like when generating a passcode of your own choice, like performing complex or long arithmetic calculation or saving details that you can access anywhere in the code. So, to demonstrate the use of key-value dictionary in bash I have given three examples that will help you in understanding that how to use them and here are those examples:

How to Perform Arithmetic operation using key-value dictionary

Another possible use of key-value dictionary is that one can perform several different arithmetic operations either complex or simple and here is the bash code that performs addition and subtraction using the data stored in dictionary:

#!/bin/bash
# Define a dictionary with key-value pairs
declare -A my_dict
my_dict["a"]=5
my_dict["b"]=10
my_dict["c"]=15

# Add two values in the dictionary
sum=$(( my_dict["a"] + my_dict["b"] ))
echo "The sum of a and b is: $sum"

# Subtract one value from another in the dictionary
diff=$(( my_dict["c"] - my_dict["a"] ))
echo "The difference between c and a is: $diff"

 

First, an associative array or dictionary named my_dict is defined with key-value pairs. Then, two values from the dictionary, a and b, are added together and stored in a variable called sum. The result of the addition is printed out using the echo command.

Next, one value from the dictionary, a, is subtracted from another value, c. The result of the subtraction is stored in a variable called diff, and the result is printed out using the echo command. Overall, this script demonstrates how to use dictionaries to store and manipulate data in Bash.

How to Create a Database Using Key-Value Dictionary

One of the most popular uses of key-value dictionaries is creating a database; it could be details of anything that you can use later in the bash script. Here I have given an example that takes the data of a company and not only that the script shows how to update an existing key in the dictionary:

#!/bin/bash
declare -A company=(
  [name]="Acme Corporation"
  [location]="New York City, NY"
  [industry]="Manufacturing"
  [size]="Large"
  [founded]="1920"
)

# print all the details
printf "Company details:\n"
for key in "${!company[@]}"; do
  printf "%s: %s\n" "$key" "${company[$key]}"
done

# modify the value of the "location" key
company["location"]="Los Angeles, CA"

# print the updated details
printf "\nUpdated company details:\n"
for key in "${!company[@]}"; do
  printf "%s: %s\n" "$key" "${company[$key]}"
done

 

This bash script defines a dictionary called company that contains information about a company, such as its name, location, industry, size, and year founded. The printf function is used to display the details of the company in a readable format. The for loop iterates over the keys of the company dictionary, and for each key, the printf function is called with the key and its corresponding value as arguments. The company dictionary is then updated by changing the value of the location key to “Los Angeles, CA”. Finally, the updated details of the company are displayed in the same format as before using another for loop and the printf function:

Conclusion

Using associative arrays can help you make your bash scripts more efficient and maintainable, by allowing you to store and retrieve data using meaningful names rather than cryptic indices. This article demonstrates the use of key-value dictionaries in Bash with the help of two examples.

About the author

Aaliyan Javaid

I am an electrical engineer and a technical blogger. My keen interest in embedded systems has led me to write and share my knowledge about them.