SC2055 – 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.

Exceptions

Rare.

if [[ $FOO != $BAR || $FOO != $COW ]]
then
    echo "$FOO and $BAR and $COW are not all equal"
fi

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