On Fri, Jan 02, 2015 at 08:15:08PM +0100, Phillip Sz wrote:
Hi,
I want to use a variable as an array name, but I dont see a way to do
this. For example:
namelength="$((address@hidden -1))"
and now I want to replace "array" with for example $1.
That's possible with namerefs:
| address@hidden ~ % bash script
| 2
| address@hidden ~ % cat script
| #!/bin/bash
|
| thearray=(foo bar baz)
|
| set -- thearray
| typeset -n key="$1"
|
| namelength="$((address@hidden -1))"
| echo "$namelength"
Or you could do it with indirection, by creating a copy of the array. Something
like:
key="address@hidden" copy=("${!key}") address@hidden
echo "$length"