Skip to main content
Clarification of original need, with summarised solutions.
Eric Smith
  • 1.3k
  • 1
  • 9
  • 16

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.

Any ideas?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.

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.

Any ideas?

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.

Eric Smith
  • 1.3k
  • 1
  • 9
  • 16

Indirect return of all elements in an array

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.

Any ideas?

close