SC2252 – ShellCheck Wiki

See this page on GitHub

Sitemap


You probably wanted && here, otherwise it's always true.

Problematic code:

if [ "$1" != foo ] || [ "$1" != bar ]
then
  echo "$1 is not foo or bar"
fi

Correct code:

if [ "$1" != foo ] && [ "$1" != bar ]
then
  echo "$1 is not foo or bar"
fi

Rationale:

This is not a bash issue, but a simple, common logical mistake applicable to all languages.

[ "$1" != foo ] || [ "$1" != bar ] is always true (when foo != bar):

[ $1 != foo ] && [ $1 != bar ] matches when $1 is neither foo nor bar:

This statement is identical to ! [ "$1" = foo ] || [ "$1" = bar ], which also works correctly (by De Morgan's law)

This warning is equivalent to SC2055 and SC2056, which trigger for intra-test expressions and arithmetic contexts respectively.

Exceptions

Rare.


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