[Top][All Lists]

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

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

From: Chet Ramey
Subject: Re: Space as a key in an associative array - different behaviours
Date: Thu, 25 Jan 2018 15:45:24 -0500
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:52.0) Gecko/20100101 Thunderbird/52.5.2

On 1/25/18 8:48 AM, Tomasz Warniełło wrote:

> Bash Version: 4.4
> Patch Level: 12
> Release Status: release
> Description:
> When trying to assign to a space key of an associative array,
> the result differs with regards to the way the operation is performed.
> 1. ((A[$a]++))
> 2. let "A[' ']++"
> 3. A[" "]=1
> I've asked a question about this here:
> https://unix.stackexchange.com/questions/419488/bash-space-as-a-key-in-an-assoc$
> 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

These two commands are basically equivalent. By the time the expression
evaluation is performed, the expression has undergone word expansion,
and the expression is "A[ ]++" or "A[ ]=1", respectively. Bash doesn't
allow blank array subscripts, and when the expression evaluator tries to
get the value of "A[ ]", it's not seen as a valid array reference.

(It should display an error message if it's not going to allow it, though.)


``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    address@hidden    http://tiswww.cwru.edu/~chet/

reply via email to

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