Bash Manual section 6.7 Arrays should mention array append notation

2022-03-08 Thread Zachary Santer
$ array=( zero one two )
$ array+=( three four five )
$ declare -p array
declare -a array='([0]="zero" [1]="one" [2]="two" [3]="three"
[4]="four" [5]="five")'
$ array=( [0]=zero [1]=one [2]=two )
$ array+=( [3]=three [4]=four [5]=five )
$ declare -p array
declare -a array='([0]="zero" [1]="one" [2]="two" [3]="three"
[4]="four" [5]="five")'
$ declare -A assoc_array=( [zero]='0' [one]='1' [two]='2' )
$ assoc_array+=( [three]='3' [four]='4' [five]='5' )
$ declare -p assoc_array
declare -A assoc_array='([four]="4" [one]="1" [five]="5" [zero]="0"
[two]="2" [three]="3" )'

Talking about the lines with "+=", obviously. I only learned I could
do this when I found it in existing code.

Regards,
Zack



Re: Bash Manual section 6.7 Arrays should mention array append notation

2022-03-08 Thread Greg Wooledge
On Tue, Mar 08, 2022 at 10:55:15AM -0500, Zachary Santer wrote:
> Talking about the lines with "+=", obviously. I only learned I could
> do this when I found it in existing code.

It's mentioned in the PARAMETERS section:

   uated.   When += is applied to an array variable using compound assign‐
   ment (see Arrays below), the variable's value is not unset  (as  it  is
   when  using  =),  and new values are appended to the array beginning at
   one greater than the array's maximum  index  (for  indexed  arrays)  or
   added  as additional key-value pairs in an associative array.  When ap‐
   plied to a string-valued variable, value is expanded  and  appended  to
   the variable's value.

It's not mentioned under Arrays, though.  Might not hurt to repeat it.