use key-value dictionary in shell script

How to Use Key-Value Dictionary in Shell Script

Dictionary is a powerful data structure supported by almost every popular programming language. They are nothing but key-value objects. They allow you to easily insert, update & retrieve data using keys. Sometimes you may need to store result in key-value dictionary in shell script. Dictionaries make it easy to store and retrieve data in a compact structure. Here is how to create dictionary, add key-value pairs, update key-value, retrieve value in Shell script.


How to Use Key-Value Dictionary in Shell Script

Here are the steps to create key-value dictionary in shell script. We will look at how to perform various operations on key-value dictionaries in shell. Please note, you need bash 4 or above to be able to use dictionaries.


1. Declare Dictionary

Here is the syntax to declare a key-value dictionary in shell script.

declare -A test_dict

declare -A statement allows you to define dictionary as associative array.


2. Add Key-Value Pairs in Dictionary

Here is how to add key-value pair of string literals without quotes.

test_dict[key1]=value1

Here is how to add key-value pair of string literals with quotes.

test_dict['key2']='value2'

You can also use shell variables as key/value. Here is an example.

key_var='key3'
value_var='value3'
test_dict[$key_var]=$value_var

You need to prefix both key and value variables with $ sign.


3. Retrieve Value from Dictionary

Here is how to retrieve values from dictionary in shell. You can use it to look up key values from your dictionary. You need to enclose dictionary variable within curly braces and prefix it with a $ sign.

echo ${test_dict[key1]}
echo ${test_dict[key2]}


4. Update Existing Key-Value in Dictionary

Updating a key-value in dictionary is easy and just like it is in other programming languages. Here is an example.

test_dict[key1]='new_value1'


5. Check if key exists in dictionary

If you need to check if a key exists in dictionary just use -v to check if that key’s value is set. Here is an example to check if key ‘abc’ exists in dictionary.

if [ -v test_dict['abc'] ]; then
    echo "abc exists in a dictionary"
fi


6. Remove key-value pair from dictionary

If you need to remove a key-value pair from dictionary, use unset statement to unset the value of a specific key.

unset test_dict[key1]
unset test_dict['key2']
unset test_dict[$key_var]


7. Iterate over a dictionary

If you want to loop through the key-value pairs in a dictionary, here is the example. It loops through the key-value pairs in dictionary and displays both key and value for each pair. Replace the dictionary name with your dictionary’s name.

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

In this article, we have looked a numerous options to work with dictionary in shell script.

Also read:

How to Block Website in Linux
How to Clear Terminal History in Linux & MacOS
How to Show Asterisk for Password in Ubuntu
How to Verify PGP Signature for Downloaded Software
How to Convert RPM to DEB File in Linux

Leave a Reply

Your email address will not be published.