Sample Header Ad - 728x90

Access values of associative array whose name is passed as argument inside bash function

1 vote
1 answer
365 views
I've some associative arrays in a bash script which I need to pass to a function in which I need to access the keys and values as well.
declare -A gkp=( \
   ["arm64"]="ARM-64-bit" \
   ["x86"]="Intel-32-bit" \
)

fv()
{
   local entry="$1"
   echo "keys: ${!gkp[@]}"
   echo "vals: ${gkp[@]}"
   local arr="$2[@]"
   echo -e "\narr entries: ${!arr}"
}

fv $1 gkp
Output for above:
kpi: arm64 x86
kpv: ARM-64-bit Intel-32-bit

arr entries: ARM-64-bit Intel-32-bit
I could get values of array passed to function, but couldn't figure out how to print keys (i.e. "arm64" "x86") in the function. Please help.
Asked by c10 (13 rep)
Sep 30, 2023, 11:21 AM
Last activity: Sep 30, 2023, 11:56 AM