Skip to content
Vidar Holen edited this page May 23, 2019 · 10 revisions

This ! is not on a condition and skips errexit. Use { ! ...; } to errexit, or verify usage.

Problematic code:

set -e
! false

Correct code:

set -e
{ ! false; }

Rationale:

ShellCheck has found a command inverted with ! that may have no effect. In particular, it does not appear as a condition in an if statement or while loop, or as the final command in a script or function.

The most common reason for this is thinking that it'll trigger set -e aka errexit if a command succeeds, as in the example. This is not the case: ! will inhibit errexit both on success and failure of the inverted command.

Wrapping such an inverted command in a brace group will trigger errexit as expected, since the brace group will act as a standalone command with the same exit code.

Exceptions:

ShellCheck will not detect cases where $? is implicitly or explicitly used to check the value afterwards:

check_success() { [ $? -eq 0 ] || exit 1; }
! false; check_success
! true; check_success

In this case, you can ignore the warning, or optionally wrap them in { ! false; }.

Related resources:

ShellCheck

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

Clone this wiki locally