diff --git a/SC2251.md b/SC2251.md index 2db85f2..0be5b47 100644 --- a/SC2251.md +++ b/SC2251.md @@ -1,4 +1,4 @@ -## This `!` is not on a condition and skips errexit. Use `&& exit 1` instead, or make sure `$?` is checked. +## This `!` is not on a condition and skips errexit. Add `|| exit 1` or make sure `$?` is checked. ### Problematic code: @@ -12,7 +12,7 @@ rest ```sh set -e -false && exit 1 +! false || exit 1 ``` ### Rationale: @@ -21,7 +21,7 @@ ShellCheck has found a command inverted with `!` that may have no effect. In par 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. -Using `&& exit ` will instead exit when failure when the command succeeds. +Adding `|| exit ` will instead exit with failure when the command succeeds. ### Exceptions: @@ -38,4 +38,5 @@ In this case, you can [[ignore]] the warning. ### Related resources: -* StackOverflow: [Why do I need parenthesis In bash `set -e` and negated return code](https://stackoverflow.com/questions/39581150/why-do-i-need-parenthesis-in-bash-set-e-and-negated-return-code/39582012) \ No newline at end of file +* StackOverflow: [Why do I need parenthesis In bash `set -e` and negated return code](https://stackoverflow.com/questions/39581150/why-do-i-need-parenthesis-in-bash-set-e-and-negated-return-code/39582012) +* [The "Use && exit 1" for SC2251 can result in a wrong exit code](https://github.com/koalaman/shellcheck/issues/3121) \ No newline at end of file