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