I have the following setup:
#! /bin/bash
init_globals() {
declare -gA global_arr1=( ["key"]="val" )
}
init_globals
echo "${global_arr1["key"]}" # WORKS! print val
local_arr1=( ["key"]="local val" )
i=1
temp=local_arr$i
current_arr=${!temp}
echo ${current_arr["key"]} # WORKS! print local val
temp=global_arr$i
current_arr=${!temp}
echo ${current_arr["key"]} # DOESN'T WORK! expect val but print nothing...
I'm trying to access globally defined associative array, based on the variable i. So I use indirect expansion to assign current_arr
to what I want. It works perfectly for an associative array defined locally. But it doesn't work with global array. Why so?