bug-bash
[Top][All Lists]
Advanced

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

Re: array subscripts act differently for integers(ie. let)


From: Chet Ramey
Subject: Re: array subscripts act differently for integers(ie. let)
Date: Wed, 18 Feb 2015 14:14:20 -0500
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:31.0) Gecko/20100101 Thunderbird/31.4.0

On 2/16/15 12:23 PM, emanuelczirai@cryptolab.net wrote:
> Oh I see, I had no idea that's how it's meant to work. My apologies.
> 
> However this case still doesn't work, but maybe I should use single quotes
> all the time?:
> 
> this fails(double quotes):
> $ declare -A ar
> $ idbad2="["
> $ let "ar[$idbad2]+=11"
> bash: let: ar[[]+=11: bad array subscript (error token is "ar[[]+=11")
> $ declare -p ar
> bash: declare: ar: not found

Bash expects the brackets to match.  This is true when the parser tries to
find the closing bracket and when the arithmetic expression evaluator
does -- they use the same criteria.  You can make a case that they should
not have to match, but bash has always behaved this way and you'll have to
work with it, at least for now.

> this works(single quotes):
> $ let 'ar[$idbad2]+=11'
> $ declare -p ar
> declare -A ar='(["["]="11" )'

This works because the arithmetic expression evaluator doesn't have to
deal with the extra `[' when trying to find the end of the subscript, and
it's smart enough to figure out whether it's dealing with an indexed or
associative array and evaluate accordingly.

> also this variant does the same:

Because (( and let are essentially equivalent.

Chet

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRU    chet@case.edu    http://cnswww.cns.cwru.edu/~chet/



reply via email to

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