But for some reasons, you maybe need it in one of your bash scripts. And of course will not rewrite the script just for that part! So you can use key-value array (And of course instead old tricks like using delimiter inside each value).
So, here how to make key-value array in Bash v4 (which comes with most Linux systems nowadays).
Initialize an empty associative array:
# Remember, key/value array is only available in Bash +4.0. declare -A locale_arrayAdd or modify elements in the array:
# One element. locale_array["English (Canada)"]=en_CA # Or many elements at the same time. locale_array+=( ["English (UK)"]=en_UK ["English (USA)"]=en_US )Get array length (note the hash at the beginning):
$ echo "${#locale_array[@]}" 3Access keys (note the exclamation mark at the beginning):
$ echo "${!locale_array[@]}" English (USA) English (Canada) English (UK)Access values:
# One value. $ echo "${locale_array[English (USA)]}" en_US # All values. $ echo "${locale_array[@]}" en_US en_CA en_UKLoop over the array:
# Loop over the array. $ for key in "${!locale_array[@]}"; do echo "- ${key} => ${locale_array[$key]}"; done - English (USA) => en_US - English (Canada) => en_CA - English (UK) => en_UKRemove an element from the array:
unset locale_array["English (UK)"]These were most common operations for arrays. Simple and easy! Thanks for Bash 4!