[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] use variable as array name
From: |
Eduardo A . Bustamante López |
Subject: |
Re: [Help-bash] use variable as array name |
Date: |
Sat, 3 Jan 2015 18:14:04 -0800 |
User-agent: |
Mutt/1.5.23 (2014-03-12) |
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"