shellcheck icon indicating copy to clipboard operation
shellcheck copied to clipboard

`SC2004`: false positive on associative arrays used inside `(())`

Open Yrahcaz7 opened this issue 1 year ago • 0 comments

For bugs

  • Rule Id: SC2004
  • My shellcheck version: online
  • [x] I tried on shellcheck.net and verified that this is still a problem on the latest commit

Here's a snippet or screenshot that shows the problem:

#!/usr/bin/env bash

char="-"
value=10

readonly -A CHAR_VALUES=(["+"]=1 ["-"]=2 ["|"]=3)
(( value += CHAR_VALUES[$char] ))

Here's what shellcheck currently says:

[Line 7:](javascript:setPosition(7, 25))
(( value += CHAR_VALUES[$char] ))
                        ^-- [SC2004](https://www.shellcheck.net/wiki/SC2004) (style): $/${} is unnecessary on arithmetic variables.

Here's what I wanted or expected to see:

Nothing, as removing the $ changes the meaning of the expression.

Note: If $char is changed to "$char", shellcheck no longer complains.

Yrahcaz7 avatar May 20 '25 01:05 Yrahcaz7