This question bad substitution shell- trying to use variable as name of array does something similar to what I need but for arrays. I'm very new to bash scripting and what I need is to do something like this:
# input
humantocheck="human1"
declare -A human1
declare -A human2
human1=( ["records_file"]="xxxxx.txt")
human2=( ["records_file"]="yyyyy.txt")
echo ${$humantocheck[records_file]}
With expected output of:
xxxxx.txt
However I get a bad substitution error when I try this.
humantocheckis not an array. If anything is going to work, it'll be a variation on${${!humantocheck}[records_file]}but I'm not confident (I didn't find the magic combo). (See shell parameter expansion.)humanfrom eitherhuman1orhuman2and then reference that. Unfortunately, I don't think there's a way to assign whole associative arrays in one swell foop. The best I've come up with uses a loop to copy the associative array element by element:for key in "${!human1[@]}"; do human[$key]="${human1[$key]}"; done. Generally speaking, using indirect variable names leads to complications, and a rethink/rewrite.