Skip to main content
Fixed formatting
Source Link
terdon
  • 252.2k
  • 69
  • 480
  • 718

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$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
$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

Rem.: Index works fine this way, no issue. Name is the issue.
 

Think I missthink I am missing something. May beMaybe 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?
 

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.
 

Think I miss something. May be 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?
 

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?

formatting
Source Link
choroba
  • 49.4k
  • 7
  • 92
  • 118

I have an array named "armin" and its name is in variable $gral (works fine)
gral="armin"

gral="armin"

Values are assigned:
declare -a ${gral}[1]="milk"
declare -a ${gral}[2]="cow"
declare ${gral}[7]="budgie"
declare ${gral}[9]="pla9ne"
fine

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

echo ${armin[@]}
milk cow budgie pla9ne

Array index exists:
echo ${!armin[@]}
1 2 7 9
Array

echo ${!armin[@]}
1 2 7 9

Array and index are fine.
 

echo $gral
armin  ## name of our bash array

echo $gral
armin ## name of our bash array
fine fine - so far.

Just to show the difference NOT using a variable:
echo ${armin[@]}
milk cow budgie pla9ne

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.

echo ${$gral[@]}
-bash: ${$gral[@]}: wrong substitution.
echo ${"$gral"[@]}
-bash: ${"$gral"[@]}: wrong substitution.
echo ${"gral"[@]}
-bash: ${"gral"[@]}: wrong substitution.
echo ${${gral}[@]}
-bash: ${${gral}[@]}: wrong substitution.

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.
 

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.

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.

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.
Source Link

retrieve bash array by referencing its name as a variable

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.

Think I miss something. May be 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?