bug-bash
[Top][All Lists]
Advanced

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

Re: Indirect expansion and arrays


From: Techlive Zheng
Subject: Re: Indirect expansion and arrays
Date: Tue, 2 Oct 2012 04:38:54 -0700 (PDT)


> On 7/29/10 4:55 PM, Bernd Eggink wrote:
> > It seems that indirect expansion doesn't work with arrays:
> > 
> > $ a=(x y z)
> > $ b=a
> > $ echo "${!b[0]} ${!b[1]} ${!b[2]}"
> > x
> > 
> > Is that intended? The documentation isn't explicit about it.

> It does, but it doesn't work in the way you are trying.  The `!' binds to
> an entire variable reference, in this case 'b[0]'.  The idea behind that
> was to permit the use of an array of variable names, for instance, that
> could be easily referenced using indirect expansion.

> The following code will display
> "x variable y variable z variable"

> a=(x y z)

> x='x variable'
> y='y variable'
> z='z variable'

> echo "${!a[0]} ${!a[1]} ${!a[2]}"

> Chet
>-- 
>``The lyf so short, the craft so long to lerne.'' - Chaucer
>                ``Ars longa, vita brevis'' - Hippocrates
> Chet Ramey, ITS, CWRU    chet@case.edu   
> http://cnswww.cns.cwru.edu/~chet/

I think that indirect reference for fash variable should be used literally.

    $ a=(x y z)
    $ b=a[@]
    $ echo "${!b}" # this would work

Combine with Chet Ramey's reply, a strucure like below would work.

    $ c=(a[0] a[1] a[2])
    $ echo "${!c[0]} ${!c[1]} ${!c[2]}" #this would work too


-- 
View this message in context: 
http://old.nabble.com/Indirect-expansion-and-arrays-tp29300541p34504291.html
Sent from the Gnu - Bash mailing list archive at Nabble.com.




reply via email to

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