Skip to content
Vidar Holen edited this page Nov 3, 2022 · 1 revision

Use && for logical AND. Single & will background and return true.

Problematic code:

if [ "$1" = "install" ] & [ "$USER" != "root" ]
then
  echo "Must be root to install"
fi

Correct code:

if [ "$1" = "install" ] && [ "$USER" != "root" ]
then
  echo "Must be root to install"
fi

Rationale:

ShellCheck found a test command followed by a &. This runs the test in the background, effectively ignoring it. To specify "logical AND" between two commands, use &&.

Exceptions:

None

Related resources:

  • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!

ShellCheck

Each individual ShellCheck warning has its own wiki page like S001. Use GitHub "Pages" feature above to find a specific one, or see Checks.

Clone this wiki locally