[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] use variable as array name

From: Jesse Molina
Subject: Re: [Help-bash] use variable as array name
Date: Sat, 03 Jan 2015 18:32:25 -0800
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:34.0) Gecko/20100101 Firefox/34.0 SeaMonkey/2.31

I found this to be helpful in regards to indirection:

echo ${!VAR}

On 1/3/15 18:14, Eduardo A. Bustamante López wrote:
On Fri, Jan 02, 2015 at 08:15:08PM +0100, Phillip Sz wrote:

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

key="address@hidden" copy=("${!key}") address@hidden
echo "$length"

reply via email to

[Prev in Thread] Current Thread [Next in Thread]