I need help with bash expansion.
I want to retrieve array values, GNU bash 5.1.0. The array name shall be a variable. "Just" referencing a variable in bash.
I have an array named "armin" and its name is in variable $gral (works fine):
gral="armin"
Values are assigned:
declare -a ${gral}[1]="milk"
declare -a ${gral}[2]="cow"
declare ${gral}[7]="budgie"
declare ${gral}[9]="pla9ne"
fine.
Array exists:
$ echo ${armin[@]}
milk cow budgie pla9ne
Array index exists:
$echo ${!armin[@]}
1 2 7 9
Array and index are fine.
I want to retrieve the array by referencing its name as a variable, not manually. Have plenty of them ...
The variable was set and used before:
$ echo $gral
armin ## name of our bash array
fine - so far.
Just to show the difference NOT using a variable:
echo ${armin[@]}
milk cow budgie pla9ne
Now attempts to reference a variable (gral) to call the name (armin):
$ echo ${$gral[@]}
-bash: ${$gral[@]}: wrong substitution.
$echo ${"$gral"[@]}
-bash: ${"$gral"[@]}: wrong substitution.
echo ${"gral"[@]}
-bash: ${"gral"[@]}: wrong substitution.
echo ${${gral}[@]}
-bash: ${${gral}[@]}: wrong substitution.
all fail. Some attempts with "eval" as well. Using associative (declare -A) makes no difference.
Rem.: Index works fine this way, no issue. Name is the issue.
I think I am missing something. Maybe the answer was described before, I found a lot of interesting stuff about variables in arrays but did not recognize an answer to my challenge.
Can you please help me find the term to retrieve the array by referencing its name as a variable?