Skip to content
Vidar Holen edited this page Apr 9, 2021 · 1 revision

In arithmetic contexts, use < instead of -lt

Similarly, > instead of -gt, <= instead of -le, >= instead of -ge, == instead of -eq, != instead of -ne.

Problematic code:

if (( 2 -lt 3 ))
then
  echo "True"
fi

Correct code:

if (( 2 < 3 ))
then
  echo "True"
fi

Rationale:

The comparators -lt, -ge, -eq and friends are flags for the test command aka [. You are instead using it in an arithmetic context, such as (( .. )) or $(( .. )), where you should be using <, >=, == etc instead.

In arithmetic contexts, -lt is simply interpreted as "subtract the value of $lt", which is clearly not the intention.

Exceptions:

If you do want to subtract $lt you can add a space to make this clear to ShellCheck: echo $((3 - lt))

Related resources:

  • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!

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