bug-bash
[Top][All Lists]
Advanced

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

Re: variable indirection with arrays


From: Chet Ramey
Subject: Re: variable indirection with arrays
Date: Wed, 07 May 2003 14:13:48 -0400

> I am trying to do this:
> 
> testarr=(1 2 3 4)
> indirect=testarr
> echo ${!indirect[@]}
> 
> And have "1 2 3 4" printed out.  This does not seem to be
> possible with my 2.05b.0 shell.  Am I missing some kind of
> syntax sauce, or are only simple variables supported with
> indirection?

The current implementation reads everything up to the character that
ends the parameter (`:', `+', `}', and so on) and uses that as the
variable name to expand for the indirect reference.  In this case,
it's going to expand `indirect[@]' to `testarr', and try to expand
`testarr'.  Since an array variable referred to without a subscript
is equivalent to referencing element 0, this will expand to `1'.

The idea was that one could load up an array with variable names, for
instance, and use indirection to examine them.

At some future point, I may change this to stop at the `[' also.  That          
would be an incompatible change, though.

Note that a future version of bash will probably adopt the ksh93 expansion
for `${!variable[@]}', which expands to the list of keys (indices) in
the array `variable'.

Chet

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
( ``Discere est Dolere'' -- chet )

Chet Ramey, ITS, CWRU    chet@po.CWRU.Edu    http://cnswww.cns.cwru.edu/~chet/




reply via email to

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