Skip to content
James edited this page Dec 1, 2025 · 5 revisions

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

and other binary operators such as

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

Optional - avoid-negated-conditions

This is an optional rule, which means that it has a special "long name" and is not enabled by default. See the optional page for more details. In short, you have to enable it with the long name instead of the "SC" code like you would with a normal rule:

.shellcheckrc

enable=avoid-negated-conditions # SC2335

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)

Related resources:

  • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
  • SC2236 and SC2237 for unary operators -n and -z

Clone this wiki locally