[Top][All Lists]

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

Re: declare a="$b" if $a previously set as array

From: Chet Ramey
Subject: Re: declare a="$b" if $a previously set as array
Date: Mon, 15 Dec 2014 21:00:35 -0500
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:31.0) Gecko/20100101 Thunderbird/31.2.0

On 12/15/14 6:41 AM, Dan Douglas wrote:

> To me the biggest problem is what happens when you explicitly request a scalar
> assignment. (I even specified a[1] to ensure it's not an "a vs. a[0]" issue.)
>       bash -c 'typeset -a a; b="(foo bar)"; typeset a[1]=$b; typeset -p a; 
> printf "<%s> " "address@hidden"; echo'
>       declare -a a='([0]="foo" [1]="bar")'
>       <foo> <bar>
> This doesn't fit with my understanding of how it should work. Otherwise I 
> think
> the way declare's arguments are interpreted based upon their form and current
> set of attributes is pretty well understood, albeit undocumented.

If you use a subscript with typeset, even when you're attempting an
assignment, the following text from the manual page applies:

"To explicitly declare an indexed array, use declare -a name (see SHELL
BUILTIN  COMMANDS  below).   declare  -a name[subscript] is also accepted;
the subscript is ignored."

Bash treats `typeset name[subscript]' as identical to `typeset -a name'
or `typeset -a name[subscript]'.

It's syntax picked up from old versions of ksh, which actually used the
subscript to size the array.  Maybe it should attempt subscript assignment,
but it never has.

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

reply via email to

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