SC2335 – ShellCheck Wiki

See this page on GitHub

Sitemap


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

and other binary operators such as

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:


ShellCheck is a static analysis tool for shell scripts. This page is part of its documentation.