Pattern: Use of string as argument to numerical operator
Issue: -
ShellCheck found a string used as an argument to a numerical operator like -eq
, -ne
, -lt
, -ge
. Such strings will be treated as arithmetic expressions, meaning n
will refer to a variable $n
, and 24/12
will be evaluated into 2
.
In the problematic example, the intention was instead to compare "n"
as a string, so it should use the equivalent string operator instead, in this case =
.
Example of incorrect code:
read -p "Continue? [y/n] " var
[ "$var" -eq "n" ] && exit 1
Example of correct code:
#read -p "Continue? [y/n] " var
[ "$var" = "n" ] && exit 1