Clone
2
SC2335
Simon Brandt edited this page 2025-08-19 10:31:33 +02:00

Use [ "$var" -ne 1 ] instead of [ ! "$var" -eq 1 ]

and other binary operators such as

  • = and != (string)
  • -le and -gt, and so forth (arithmetic)

Problematic code:

if [ ! "$var" -eq 1 ]; then :; fi
if ! [ "$var" = foo ]; then :; fi

Correct code:

if [ "$var" -ne 1 ]; then :; fi
if [ "$var" != foo ]; then :; fi

Rationale:

Double negation of such binary operators is unnecessary.

Exceptions:

This is a stylistic issue that does not affect correctness. If you prefer the original expression, you can Ignore it with a directive or flag.

This rule is not applied to the followings:

  • < and > (lexicographical)
  • -nt and -ot (file timestamp)
  • =~ (regular expression)
  • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
  • SC2236 and SC2237 for unary operators -n and -z