Use hash table in shell
Declare a hash array and assign:
1 | declare -A animals = ( ["moo"]="cow" ["woof"]="dog" ) |
Insert key-value pair or modify value by key:
1 | animals[meow]="cat" |
Get keys and values by iterator:
1 2 3 | for sound in "${!animals[@]}"; do echo "$sound - ${animals[$sound]}"; done |
You can use hash arrays just like normal arrays. “${animals[@]}” expands the values, “${!animals[@]}” (notice the !) expands the keys.
Reference: