Composite Types: Associative Arrays
Learn how to declare and use associative arrays.
We'll cover the following...
Associative arrays
The elements of indexed arrays are strings. Each element has an index that is a positive integer. The indexed array gives us a string by its index.
The developers introduced associative arrays in the fourth version of Bash. These arrays use strings as element indexes instead of integers. This kind of string index is called the key. The associative array gives us a string value by its string index. When do we need this feature?
Let’s suppose we need a script that stores a list of contacts. The script adds a person’s name, email, or phone number to the list. Let’s omit the person’s last name for simplicity. When we need this data back, the script prints it on the screen.
We can solve the task using an indexed array. This solution would be inefficient to search for the required contact. The script should traverse over all array elements and compare each element with the person’s name that we are ...