The Bash man page describes use of ${!a}
to return the contents of the variable whose name is the contents of a
(a level of indirection).
I'd like to know how to return all elements in an array using this, i.e.,
a=(one two three) echo ${a[*]}
returns
one two three
I would like for:
b=a echo ${!b[*]}
to return the same. Unfortunately, it doesn't, but returns 0
instead.
Update
Given the replies, I now realise that my example was too simple, since of course, something like:
b=("${a[@]}")
Will achieve exactly what I said I needed.
So, here's what I was trying to do:
LIST_lys=(lys1 lys2) LIST_diaspar=(diaspar1 diaspar2) whichone=$1 # 'lys' or 'diaspar' _LIST=LIST_$whichone LIST=${!_LIST[*]}
Of course, carefully reading the Bash man page shows that this won't work as expected because the last line simply returns the indices of the "array" $_LIST
(not an array at all).
In any case, the following should do the job (as pointed out):
LIST=($(eval echo \${$_LIST[*]}))
or ... (the route that I went, eventually):
LIST_lys="lys1 lys2" ... LIST=(${!_LIST})
Assuming, of course, that elements don't contain whitespace.
[@]
to the pointer_LIST="LIST_${whichone}[@]"
and then, useLIST=("${!_LIST}")
to copy the array. It is a good idea to use lower case variable names to avoid conflicts with environment variables (All caps).