Skip to content

Files

Latest commit

 

History

History
37 lines (25 loc) · 851 Bytes

SC2170.md

File metadata and controls

37 lines (25 loc) · 851 Bytes

Pattern: Comparing a string value with a numerical operator

Issue: -

Description

You are comparing a string value with a numerical operator, such as -eq, -ne, -lt or -gt.

In [[ .. ]], this would automatically dereference the string, looking to see if there are variables by that name.

In [ .. ], which you are using, the string is just treated as an invalid number.

If you want to compare numbers, expand yourself (e.g. use $var instead of var). If you are trying to compare strings and not numbers, use =, != \< or \> instead.

Example of incorrect code:

read -r n
if [ n -lt 0 ]
then
   echo "bad input"
fi

Example of correct code:

read -r n
if [ "$n" -lt 0 ]
then
   echo "bad input"
fi

Further Reading