The values of an associative array are accessed using the following syntax ${ARRAY[@]}.
To access the keys of an associative array in bash you need to use an exclamation point right before the name of the array: ${!ARRAY[@]}.
To iterate over the key/value pairs you can do something like the following example
# For every key in the associative array..
for KEY in "${!ARRAY[@]}"; do
# Print the KEY value
echo "Key: $KEY"
# Print the VALUE attached to that KEY
echo "Value: ${ARRAY[$KEY]}"
done
NOTE: The use of quotes around the array of keys ${!ARRAY[@]} in the for statement (plus the use of @ instead of *) is necessary in case any keys include spaces.
The following script can be used to print colored text in bash. You can use it in any script without copy pasting everything in it by executing the following command source cecho.sh. Doing so, it will load to your script the functions that are defined in cecho.sh, making them available…
The following code will connect to a MySQL server, it will get a list of integers and convert the results to a bash array that will be iterated using a for loop and they will be printed using zero padding.
The following function takes one argument - a text file.The text file should contain one word on each line.The function reads the text file (argument) line by line.Then it checks if the line has one word; if this is true, it splits the word in half.Finally, it prints the two…