Lazy pro usage of bash associative arrays (dictionaries)
This is an example on how to leverage dictionaries like contraptions in bash.
Tested on RHEL 7, Ubuntu 14.04, 16.04
#!/bin/bash set -e ASSOC_ARRAY_KEYS=("KEY1" "KEY2") KEY1_VAL="VAL1" KEY2_VAL="VAL2" for ASSOC_ARRAY_KEY in ${ASSOC_ARRAY_KEYS[@]}; do VAL_REF="${ASSOC_ARRAY_KEY}_VAL" echo "${!VAL_REF}" # it should print VAL1 and VAL2 # if you want to change the value of a certain key NEW_VALUE_FOR_KEY1="VAL_NEW" eval $VAL_REF=\"\$\{NEW_VALUE_FOR_KEY1\}\" echo "${!VAL_REF}" # it should print VAL_NEW done
That's all, folks!
Tweet