Skip to content
Michael Warkentin edited this page Sep 26, 2017 · 5 revisions

> is for string comparisons. Use -gt instead.

Problematic code:

if [[ $var > 10 ]]
then
  echo "Incorrectly triggers when var=5"
fi

Correct code:

if [[ $var -gt 10 ]]
then
  echo "Correct numerical comparison"
fi

Rationale:

< and >, in both [[ and [ (when escaped) will do a lexicographical comparison, not a numerical comparison.

This means that [[ 5 > 10 ]] is true because 5 comes after 10 alphabetically. Meanwhile [[ 5 -gt 10 ]] is false because 5 does not come after 10 numerically.

If you want to compare numbers by value, use the numerical comparison operators -gt, -ge, -lt and -le.

Exceptions:

If the strings happen to be version numbers and you're using <, or > to compare them as strings, and you consider this an acceptable thing to do, then you can ignore this warning.

ShellCheck

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.

Clone this wiki locally