[ ]
does not apply arithmetic evaluation. Evaluate with $((..))
for numbers, or use string comparator for strings.
Problematic code:
[ 2*3 -eq array[i] ]
Correct code:
[ $((2*3)) -eq $((array[i])) ]
Rationale:
When using [[ .. ]]
with numerical comparators (-eq
, -lt
, etc), the value on either side will be evaluated as an arithmetic expression. This means that 2*3
will be evaluated to 6
, and x
will be evaluated to the contents of the variable $x
.
When using [ .. ]
, this does not happen. 2*3
and x
will both be considered invalid numbers. Instead, use e.g. $((2*3))
to evaluate the expression before passing it to [ .. ]
.
Alternatively, if the expression should be considered a string, quote the expression and use a string comparison operator like =
and !=
.
Exceptions:
None
Related resources:
- Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
-
Installation
-
Usage
-
Integrating and extending
Each individual ShellCheck warning has its own wiki page like SC1000. Use GitHub Wiki's "Pages" feature above to find a specific one, or see Checks.
<--- This is a global footer, please don't edit it. --->