[Top][All Lists]

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

Re: double array var expansion

From: Chet Ramey
Subject: Re: double array var expansion
Date: Sat, 04 Mar 2006 17:14:53 -0500
User-agent: Thunderbird 1.5 (Macintosh/20051201)

address@hidden wrote:
> Help!
> Why does this not work?
> n=a
> a=( x y z)
> echo "$!n[0]"
> echo "$!n[1]"
> echo "$!n[2]"
> only value i get is a[0]

First of all, you need the braces.  Otherwise you get $!, followed by
n[0], n[1], and n[2], respectively.

Second, once you add the braces, the entire parameter is indirected:
n[0], n[1], or n[2].  Only the first expands to anything, since
subscripting a scalar with 0 is equivalent to expanding it without
the array syntax.  So you get 'a', since $n == a, and try to indirect
through `$a'.  Since referencing an array variable without using array
syntax is equivalent to referencing element 0, you get a[0], or x.

Bash uses the entire rest of the parameter portion of the expansion
as the part to expand and perform indirection on; this is documented in
the manual page.

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

reply via email to

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