I want to have a global associative array, that is filled at several locations and I can not get it to work to initialize the array with the content of a string without using the declare -A -g over and over at said locations(which I don't feel like is the smartest approach). I've extracted the issue to the code below:
#!/bin/bash
declare -A arr
declare inputString
inputString="([key]=val)"
arr="$inputString"
printf "from variable: ${arr[*]}\n"
printf "from variable: ${arr[key]}\n"
arr=([key]=val)
printf "inline: ${arr[*]}\n"
printf "inline: ${arr[key]}\n"
declare -A arr=$inputString
printf "with declare: ${arr[*]}\n"
printf "with declare: ${arr[key]}\n"
the output is:
from variable: ([key]=val)
from variable:
inline: val
inline: val
with declare: val
with declare: val
whereas I would expect it to be:
from variable: val
from variable: val
inline: val
inline: val
with declare: val
with declare: val
what do I have to do to accomplish this task?
${!varname[key]}