[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] use variable as array name
From: |
phillip |
Subject: |
Re: [Help-bash] use variable as array name |
Date: |
Mon, 05 Jan 2015 11:19:45 +0100 |
User-agent: |
K-9 Mail for Android |
Thanks a lot!
Phillip
On 4 January 2015 03:14:04 CET, "Eduardo A. Bustamante López" <address@hidden>
wrote:
>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"