Skip to content

Files

Latest commit

 

History

History
27 lines (16 loc) · 896 Bytes

SC2255.md

File metadata and controls

27 lines (16 loc) · 896 Bytes

Pattern: Use of [[ .. ]] with numerical comparator

Issue: -

Description

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 !=.

Example of incorrect code:

[ 2*3 -eq array[i] ]

Example of correct code:

[ $((2*3)) -eq $((array[i])) ]

Further Reading