bug-bash
[Top][All Lists]
Advanced

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

Re: Using variables in variables names


From: Mike Stroyan
Subject: Re: Using variables in variables names
Date: Mon, 13 Mar 2006 15:07:18 -0700

On 3/13/06, Paul Jarc <prj@po.cwru.edu> wrote:
> "Dirk H. Schulz" <dirk.schulz@kinzesberg.de> wrote:
> > Paul Jarc schrieb:
> >> ac=12 eval "dings$ac=wasannersder"
> >
> > And how do I reference it then?
>
> ac=12 eval "value=\$dings$ac"
> echo $value
>
> Or:
>
> ac=12 name=dings$ac echo ${!name}

It seems that you need to use the eval form instead of the ${!var} form
to handle array variables.  Here are some examples I played with.  The
pattern is to use a backslash to quote the $ for the array name.  The $i
in the array examples could be done as \$i because it works out the same
if it is expanded in either the first pass or the second pass.

$ suffix=one
$ eval "pre_${suffix}=simple1"
$ suffix=two
$ eval "pre_${suffix}=simple2"
$ suffix=one
$ eval "echo \$pre_${suffix}"
simple1
$ suffix=two
$ eval "echo \$pre_${suffix}"
simple2
$ suffix=one
$ i=1
$ eval "pre_A_${suffix}[$i]=array1_1"
$ i=2
$ eval "pre_A_${suffix}[$i]=array1_2"
$ suffix=two
$ i=1
$ eval "pre_A_${suffix}[$i]=array2_1"
$ i=3
$ eval "pre_A_${suffix}[$i]=array2_3"
$ set | grep pre_
_='pre_A_two[3]=array2_3'
pre_A_one=([1]="array1_1" [2]="array1_1")
pre_A_two=([1]="array2_1" [3]="array2_3")
pre_one=simple1
pre_two=simple2
$ i=1
$ eval "echo \${pre_A_${suffix}[$i]}"
array2_1
$ eval "echo \${pre_A_${suffix}[$i]}"
array2_1
$ i=3
$ eval "echo \${pre_A_${suffix}[$i]}"
array2_3
$ i=2
$ suffix=one
$ eval "echo \${pre_A_${suffix}[$i]}"
array1_2

--
Mike Stroyan
stroyan@gmail.com




reply via email to

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