What I have is an array with some variables. I can iterate to get the values of those vars but what I need is actually their names (values will be used elsewhere).
Going with var[i] won't work cause I will have different names. I guess I could workaround this by creating another array with the names - something similar to this: Getting variable values from variable names listed in array in Bash
But I'm wondering if there is a better way to do this.
var1=$'1'
var2=$'2'
var3=$'3'
Array=( $var1 $var2 $var3)
for ((i=0; i<${#Array[@]}; i++))
do
echo ${Array[i]}
done
Is:
>1
>2
>3
Should be:
>var1
>var2
>var3
var1var2var3in your array?var1=1; var2=2; var3=3; Array=( $var2 $var2 $var3 )results inArray=( 1 2 3 ). The entries aren't linked to variables in any significant way -- if you then afterwards ranvar1=4; var2=5; var3=6, the array would still be1 2 3; as such, there isn't any metadata to retrieve.$'1'is exactly the same as1; there's no point whatsoever to using the alternate literal syntax here -- in most of the cases where there was a point to using that syntax, you'd need to make itArray=( "$var1" "$var2" "$var3" )to avoid munging your data during the assignment).var1=1; var2=2; var3=3; Array=[ var1, var2, var3 ], you can't get back fromArray[0]'s value of1to the variablevar1it was originally taken from (especially sincevar1may no longer have the value1at the future point in time when the inspection is desired).