[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Space as a key in an associative array - different behaviours
From: |
Greg Wooledge |
Subject: |
Re: Space as a key in an associative array - different behaviours |
Date: |
Thu, 25 Jan 2018 09:51:13 -0500 |
User-agent: |
NeoMutt/20170113 (1.7.2) |
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.