bug-bash
[Top][All Lists]
Advanced

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

Re: bash 3.2.9, constructing array references with indirect expansion


From: Chet Ramey
Subject: Re: bash 3.2.9, constructing array references with indirect expansion
Date: Thu, 13 Sep 2007 22:05:26 -0400
User-agent: Thunderbird 2.0.0.6 (Macintosh/20070728)

Misfortunado Farmbuyer wrote:
> I've been staring at Chet's message in
> http://www.mail-archive.com/bug-bash@gnu.org/msg01545.html
> for a while, and now I understand why my own script (doing something
> similar) was not originally working.  What I can't quite figure is what
> to change.
> 
> I'm source'ing a series of assignments of the form
>       DIRS_name1=(/foo /bar /baz)
>       DIRS_name2=(/qux /quux)
>       ....
> and earlier in the file, a list of the arbitrary 'name's are assigned.
> Running through the list of 'name's and composing the corresponding array
> variable name is no trouble, but I can't manage to indirect through to the
> entire array.  Like the person I linked to above, I keep ending up with only
> the first member of the array:

You have to remember that referencing an array variable without using
${name[@]} will always return the first element of the array.  Indirect
variable expansion cannot be coerced to do otherwise.

You can always use `eval' to force the sort of double expansion you
want.  Just remember to quote everything whose evaluation you want
deferred:

DIRS_name1=( /foo /bar /baz )
DIRS_name2=( /qux /quux )

name=${1:-name1}

v=DIRS_$name

eval a='( "${'$v'[@]}" )'

echo ${a[@]}

Chet

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
                       Live Strong.  No day but today.
Chet Ramey, ITS, CWRU    chet@case.edu    http://cnswww.cns.cwru.edu/~chet/




reply via email to

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