Bash 4.3-alpha introduced the nameref attribute, which could be used in this case:
declare -A arr1=([foo]=bar)
declare -A arr2=([foo]=baz)
arrays=(arr1 arr2)
for idx in "${arrays[@]}"; do
declare -n temp="$idx"
echo "${temp[foo]}"
done
gives the output
bar
baz
As pointed out by kojiro in his comment, storing the array names in an array to iterate over is not actually required as long as the names have a shared prefix.
arrays=(arr1 arr2)
for idx in "${arrays[@]}"; do
could be replaced by
for idx in "${!arr@}"; do
Notice that despite the exclamation mark, this has nothing to do with indirect expansion.
Relevant excerpts from the reference manual
Section "Shell Parameters":
A variable can be assigned the nameref attribute using the -n
option to the declare
or local
builtin commands (see Bash
Builtins)
to create a nameref, or a reference to another variable. This allows
variables to be manipulated indirectly. Whenever the nameref variable
is referenced or assigned to, the operation is actually performed on
the variable specified by the nameref variable's value. A nameref is
commonly used within shell functions to refer to a variable whose name
is passed as an argument to the function. For instance, if a variable
name is passed to a shell function as its first argument, running
declare -n ref=$1
inside the function creates a nameref variable ref
whose value is
the variable name passed as the first argument. References and
assignments to ref
are treated as references and assignments to the
variable whose name was passed as $1
.
If the control variable in a for
loop has the nameref attribute, the
list of words can be a list of shell variables, and a name reference
will be established for each word in the list, in turn, when the loop
is executed. Array variables cannot be given the -n
attribute.
However, nameref variables can reference array variables and
subscripted array variables. Namerefs can be unset using the -n
option to the unset
builtin (see Bourne Shell
Builtins).
Otherwise, if unset
is executed with the name of a nameref variable
as an argument, the variable referenced by the nameref variable will
be unset.
Section "Shell Parameter Expansion":
${!prefix*}
${!prefix@}
Expands to the names of variables whose names begin with prefix
,
separated by the first character of the IFS
special variable. When
@
is used and the expansion appears within double quotes, each
variable name expands to a separate word.