bug-bash
[Top][All Lists]
Advanced

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

Re: Space as a key in an associative array - different behaviours


From: Tomasz Warniełło
Subject: Re: Space as a key in an associative array - different behaviours
Date: Thu, 25 Jan 2018 16:54:07 +0100

(Sorry for my previous mail. I sent it too early by mistake. Please delete
it. Here's the full version.)

Some single quotes...

`let` is more consistent with them:

$ unset A; a=" ";declare -A A; let "A['$a']++"; declare -p A
declare -A A=([" "]="1" )
$ unset A; a=" ";declare -A A; let "A[''$a]++"; declare -p A
declare -A A=([" "]="1" )
$ unset A; a=" ";declare -A A; let "A[$a'']++"; declare -p A
declare -A A=([" "]="1" )

than the double parentheses:

$ unset A; a=" ";declare -A A; ((A['$a']++)); declare -p A
declare -A A=([" "]="1" )
$ unset A; a=" ";declare -A A; ((A[''$a]++)); declare -p A
declare -A A
$ unset A; a=" ";declare -A A; ((A[$a'']++)); declare -p A
declare -A A

For the record.

2018-01-25 15:51 GMT+01:00 Greg Wooledge <address@hidden>:

> On Thu, Jan 25, 2018 at 02:48:47PM +0100, Tomasz Warniełło wrote:
> > Repeat-By:
> > 1.
> > $ unset A; a=" ";declare -A A; ((A[$a]++)); declare -p A
> > declare -A A
> >
> > 2.
> > $ unset A; a=" ";declare -A A; let "A[$a]=1"; declare -p A
> > declare -A A
> >
> > 3.
> > $ unset A; a=" ";declare -A A; A[$a]=1; declare -p A
> > declare -A A=([" "]="1" )
>
> The first two are using a math context.  If you want the $a to survive
> intact in this context, to make it to the array, you need some single
> quotes.
>
> wooledg:~$ unset A; a=" ";declare -A A; (('A[$a]++')); declare -p A
> declare -A A=([" "]="1" )
>
> wooledg:~$ unset A; a=" ";declare -A A; let 'A[$a]=1'; declare -p A
> declare -A A=([" "]="1" )
>
> Otherwise, the parser gets all confused.  I'll let someone else attempt
> a more technical explanation.
>
> The third example was not using a math context; it was just a simple
> assignment statement.  That gets parsed entirely differently.
>


reply via email to

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