bug-bash
[Top][All Lists]
Advanced

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

Re: Weirdness in associative array assignements


From: Chet Ramey
Subject: Re: Weirdness in associative array assignements
Date: Wed, 15 Oct 2014 21:10:47 -0400
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:24.0) Gecko/20100101 Thunderbird/24.6.0

On 10/15/14, 4:55 AM, dnade.ext@orange.com wrote:
>             Hello dear bash-bug mailing-list!
> 
> I am puzzled by bash behavior with array assignments.
> 
> It seems there is a behavior change introduced in bash 4.3 in array 
> assignments. An array can actually reference itself in its own declaration.
> 
> $ declare -A foo=([bar]=1st) ; foo=([bar]=2nd [qwer]=${foo[bar]}) ; echo 
> "${foo[@]}"
> 2nd 2nd
> $ bash --version
> GNU bash, version 4.3.11(1)-release (x86_64-pc-linux-gnu)

This changed back in May, 2011 in response to this bug report:

http://lists.gnu.org/archive/html/bug-bash/2011-05/msg00005.html

The idea is that associative array assignments using the compound array
syntax should behave more like sequential assignments to the array.  That
is, foo=([bar]=2nd [qwer]=${foo[bar]}) should be like

foo[bar]=2nd ; foo[qwer]=${foo[bar]}

The change fixed a problem with double expansion and quote removal being
performed on the indices.  ksh93 does things the same way.


> 
> $ declare -a foo=(1st) ; foo=(2nd ${foo[0]}) ; echo ${foo[@]}
> 2nd 1st
> $ bash --version
> GNU bash, version 4.3.11(1)-release (x86_64-pc-linux-gnu)
> 
> $ declare -a foo=(1st) ; foo=(2nd ${foo[0]}) ; echo ${foo[@]}
> 2nd 1st
> $ bash --version
> GNU bash, version 4.2.25(1)-release (x86_64-pc-linux-gnu)

I'll take a look at this, but bash and ksh93 do this the same way.

> Another weirdness (I think) is this:
> $ declare -A foo=([bar]=asdf [baz]=qwer) ; foo+=([bar]=asdf [baz]=qwer) ; 
> echo "${!foo[@]}" "${foo[@]}"
> bar baz asdfasdf qwerqwer

I think this behavior is useful.  If you want to overwrite values, there is
an easy way to do it, but there's value in making
foo+=([bar]=asdf [baz]=qwer) equivalent to foo[bar]+=asdf ; foo[baz]+=qwer
as above.

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
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]