Updated SC2251 (markdown)

Vidar Holen
2019-07-04 19:12:23 -07:00
parent 8b2088d3be
commit 27a916fd00

@@ -11,7 +11,7 @@ set -e
```sh ```sh
set -e set -e
{ ! false; } false && exit 1
``` ```
### Rationale: ### Rationale:
@@ -20,19 +20,20 @@ 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. 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. Using `&& exit ` will instead exit when failure when the command succeeds.
### Exceptions: ### Exceptions:
ShellCheck will not detect cases where `$?` is implicitly or explicitly used to check the value afterwards: ShellCheck will not detect cases where `$?` is implicitly or explicitly used to check the value afterwards:
``` ```
set -e;
check_success() { [ $? -eq 0 ] || exit 1; } check_success() { [ $? -eq 0 ] || exit 1; }
! false; check_success ! false; check_success
! true; check_success ! true; check_success
``` ```
In this case, you can [[ignore]] the warning, or optionally wrap them in `{ ! false; }`. In this case, you can [[ignore]] the warning.
### Related resources: ### Related resources: