Bash eval array variable name

Below is my bash instance:

First instance, this is what I intend to do "aliasing" var with myvarA:

myvarA="variableA"
varname="A"
eval varAlias=\$"myvar"$varname
echo $varAlias

2nd instance for array variable and also knotting its participants, which is unimportant:

myvarA=( "variableA1" "variableA2" )
for varItem in ${myvarA[@]}
do
    echo $varItem
done

Currently in some way I require to make use of "aliasing" strategy like instance 1, yet this moment for array variable:

eval varAlias=\$"myvar"$varname
for varItem in ${varAlias[@]}
do
    echo $varItem
done

But also for last instance, just first participant of myvarA is published, which is eval review to value of the variable, just how need to I do var array variable so eval is review to the name of array variable not the value of the variable.

0
2019-05-04 05:32:43
Source Share
Answers: 2

In your solution, varAlias isn't an array, so you can do for varItem in $varAlias which is simply doing word splitting. As a result of that if any one of your initial array components include rooms, they will certainly be dealt with as different words.

You can do scalar indirection similar to this : a=42; b=a; echo ${!b}.

You can do indirection to an array of scalars similar to this :

$ j=42; k=55; m=99
$ a=(j k m)
$ echo ${!a[1]}
55

However, there's no sufficient means to do the sort of array indirection you're attempting to do. You need to have the ability to revamp your code to make sure that no indirection is required.

See additionally BashFAQ/006.

0
2019-05-08 00:53:09
Source

I addressed it; last instance needs to resemble this :

eval varAlias=\${"myvar"$varname[@]}
for varItem in ${varAlias[@]}
do
    echo $varItem
done
.
0
2019-05-07 21:46:16
Source