- Each key in the array can only appear once. You can think of it as a unique ID for a user in a list.
- A value can appear more than once in an array. For example, two persons in a list can have the same name but need to have different user IDs.
In this article, we will explain how you can declare and initialize associative arrays in Linux bash. We will further elaborate on the power of the associative arrays with the help of various examples.
We have run the examples mentioned in this article on a Debian 10 Buster system. However, you can easily replicate on almost all Linux distros.
Verifying the Pre-requisites
For using Associative Arrays on Linux Bash, your GNU Bash version has to be equal to or higher than version 4. Open your Linux Terminal by accessing it through the Application Launcher search. Then enter the following command to check your installed version of bash:
My current bash version is 5.0.3 so I am good to go. In case your bash version is less than 4, you can upgrade bash by running the following command as sudo:
Declaring an Associative Array and Initialize It
Declaring an Associative array is pretty simple in bash and can be be done through the declare command:
In our example, we will be declaring an array variable named sampleArray1 as follows:
The next step is to initialize the required values for your array. In our example, we want to have an array where values are a few country names and the keys are their relevant country name abbreviations. Here, we will feed the array values, one by one as follows:
Example:
$ sampleArray1[JPN]=Japan
$ sampleArray1[KOR]=Korea
$ sampleArray1[TWN]=Taiwan
$ sampleArray1[TH]=Thailand
A quick alternative is to declare and initialize an array in a single bash command as follows:
Here is how we can declare and initialize our mentioned array, alternatively, as follows:
[TH]=Thailand )
Now we will present some examples that will elaborate on what all you can do with Associative Arrays in bash:
Example1: Accessing the array keys and values
In this example we will explain how you can:
- Print a value against a key
- Print all array keys at once
- Print all array values at once
And,
- Print all the key-value pairs at once
You can print a value against a key by using the following command syntax:
Here is how we can access a country’s full name by providing the country’s name abbreviation, from our sampleArray1:
$ echo ${sampleArray1[TWN]}
If you are interested in printing all keys of your associative array, you can do so using the following syntax:
The following command will print all country name abbreviations from my sampleArray1 by
using a for loop:
Another alternative to printing all keys from the array is by using parameter expansion. The following command will print all keys in the same line:
If you are interested in printing all the array values at once, you can do so by using the for loop as follows:
The following command will print all full country names stored in my sampleArray1:
Another alternative to printing all values from the array is by using parameter expansion. The following command will print all values in the same line:
The next useful example will print all the key-value pairs at once by using the for loop as follows:
${sampleArray1[$key]}"; done
You can, of course, make this information retrieval more useful in your complex and meaningful bash scripts.
Example 2: Counting Array items
The following command can be used to count and print the number of elements in your associative array:
The output of the following command shows that I have five items in my sampleArray1:
Example 3: Adding new data in Array
If you want to add an item to an array after you have already declared and initialized it, this is the syntax you can follow:
In my example, I want to add another country along with its county name abbreviation so I will use the following command:
Echoing the array values now suggests that the new country is added to my array:
Example 4: Deleting Item from Array
By unsetting an entry from the associative array, you can delete it as an array item. This is the unset syntax use can use in order to do so:
In my example, I want to remove the key-value pair “AL-Alabama” from my array so I will unset the “AL” key in my command:
Echoing the array values now suggests that the AL-Alabama key-value is now removed from my array:
Example 5: Verifying If an item exists in the array
By using the if condition in the following manner, you can verify if an item is available in your associative array or now:
For example, if I check if the recently deleted AL-Alabama item exists in my array, the following message will be printed:
If I check for an item that exists, the following result will be printed:
Example 6: Deleting an Array
You can delete an Associative Array from your bash memory by using the unset command as follows:
By using the following simple command, I will delete my sampleArray1 from the memory:
Now, when I try to print all the array values through the following command, I get none.
By using these examples in your Linux bash scripts, you can use the power of the associative arrays to achieve a solution to many complex problems.
from Linux Hint https://ift.tt/2IyXtJH
0 Comments