bug-bash
[Top][All Lists]
Advanced

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

Re: Array variables and variable indirection


From: Chet Ramey
Subject: Re: Array variables and variable indirection
Date: Mon, 19 Aug 2002 15:40:57 -0400

> Bash 2.05.0(1)-release
> Slackware 8.0, Intel i386 hardware, Linux 2.4.18, libc 2.2.3
> Compiler unknown, presumably GCC 2.95.3
> 
> It doens't seem to be possible to access array variables through variable
> indirection.

It's a question of how much of the parameter following the `!' that
you want to use.  Bash treats everything up to the end of the
parameter (in this case, everything up to the `}') as the variable to
be expanded to get the variable name on which to do the indirection. 

> > declare -a
> declare -ar BASH_VERSINFO='([0]="2" [1]="05" [2]="0" [3]="1" [4]="release" 
> [5]="i386-slackware-linux-gnu")'
> [... delitia ...]
> > array=BASH_VERSINFO
> > echo ${array}
> BASH_VERSINFO
> > echo ${!array}
> 2
> # so far, so good, ${!array} is acted on exactly like ${BASH_VERSINFO}
> # would, which is the same as ${BASH_VERSINFO[0]} in this case.
> > echo ${!array[*]}
> 2
> # Now what's happening?
> # Shouldn't that have been "2 05 0 1 release i386-slackware-linux-gnu",
> # not "2"?

No.  What happens is that the expansion of `array[*]' is used to find
the variable name to `indirect on'.  This expands to BASH_VERSINFO, since
`array' is a scalar variable and referencing a scalar variable as an
array with subscript 0, *, or @ returns the variable's value.  The
expansion then becomes ${BASH_VERSINFO}, which, as you noted, is equivalent
to ${BASH_VERSINFO[0]}, or 2.

> # Shouldn't that be "release", not a blank line?

No, for the same reason.

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]