On Mon, Mar 29, 2021 at 4:18 PM Chet Ramey <chet.ra...@case.edu> wrote:
> If you look at
>
> a=( k1 v1 k2 v2 k3 v3)
>
> as more or less syntactic sugar for
>
> a=( [k1]=v1 [k2]=v2 [k3]=v3 )
>
> it's reasonable that
>
> a=( k1 v1 k2 )
>
> is equivalent to
>
> a=( [k1]=v1 [k2]= ). And that's what bash does.

This equivalence, though it is reasonable, creates a special case for the last
value in an array which imposes a greater cognitive burden on the programmer.
The programmer needs to be aware that omitting the last value will create an
empty value rather than emitting an error. Given that you need quotes to create
an empty value for any other element but the last:

  $ a=( k1 "" k2 v2 k3)
  $ declare -p a
  declare -A a=([k1]="" [k2]="v2" [k3]="" )

I would prefer the consistency and readability of always requiring an even
number of arguments:

  $ a=( k1 "" k2 v2 k3 "")
  $ declare -p a
  declare -A a=([k1]="" [k2]="v2" [k3]="" )

In general I find that fewer surprises in a language lead to easier to grok
code.

Reply via email to