On Fri, Nov 28, 2014 at 03:47:49PM -0500, Maarten Billemont wrote: > What I would expect is for += inside +=() to behave as = does now, and = to > behave as it does outside of +=(), which is to "set" the value, not > append. Ergo: > > declare -a indexed_array=( [0]=a ) > indexed_array=( [0]=b ) #=> [0]=b -- because we unset the array and set > the element so those given. > indexed_array+=( [0]=b ) #=> [0]=b -- because we mutate the array by > setting the element to that given. > indexed_array=( [0]+=b ) #=> [0]=b -- because we unset the array and set > the element by appending the given to nothing. > indexed_array+=( [0]+=b ) #=> [0]=bb -- because we mutate the array by > appending the given to what is already there.
Further expanding on this: | dualbus@hp ~ % cat array | #!/bin/bash | | for shell in \ | ksh93 \ | ~/local/bin/bash \ | /tmp/bash/bin/bash; do | echo "$shell" | "$shell" -c ' | typeset -A A | A=( ["k"]=a ) | echo "A=([\"k\"]=b)" | A=(["k"]=b); typeset -p A | A=( ["k"]=a ) | echo "A+=([\"k\"]=b)" | A+=(["k"]=b); typeset -p A | A=( ["k"]=a ) | echo "A=([\"k\"]+=b)" | A=(["k"]+=b); typeset -p A | A=( ["k"]=a ) | echo "A+=([\"k\"]+=b)" | A+=(["k"]+=b); typeset -p A | ' | done | dualbus@hp ~ % ./array | ksh93 | A=(["k"]=b) | typeset -A A=([k]=b) | A+=(["k"]=b) | typeset -A A=([k]=b) | A=(["k"]+=b) | typeset -A A=([k]=b) | A+=(["k"]+=b) | typeset -A A=([k]=ab) | /home/dualbus/local/bin/bash | A=(["k"]=b) | declare -A A='([k]="b" )' | A+=(["k"]=b) | declare -A A='([k]="ab" )' | A=(["k"]+=b) | declare -A A='([k]="b" )' | A+=(["k"]+=b) | declare -A A='([k]="ab" )' | /tmp/bash/bin/bash | A=(["k"]=b) | declare -A A='([k]="b" )' | A+=(["k"]=b) | declare -A A='([k]="b" )' | A=(["k"]+=b) | declare -A A='([k]="b" )' | A+=(["k"]+=b) | declare -A A='([k]="ab" )' ~/local/bin/bash is bash from the devel branch. /tmp/bash/bin/bash is bash with the attached patch applied. The attached patch makes bash behave like ksh93, at least for the associative arrays case, because in the case of normal arrays, ksh93 does some pretty funny things: | dualbus@hp ~ % ksh93 -c 'a=(); a+=([0]=b); typeset -p a' | typeset -A a=([0]=b) # Ha! Now 'a' is an associative array...
diff --git a/arrayfunc.c b/arrayfunc.c index 804e6da..0f900aa 100644 --- a/arrayfunc.c +++ b/arrayfunc.c @@ -498,7 +498,7 @@ assign_compound_array_list (var, nlist, flags) for (list = nlist; list; list = list->next) { - iflags = flags; + iflags = (flags & ~ASS_APPEND); w = list->word->word; /* We have a word of the form [ind]=value */