From 77d90bf5c8ea03f81feee01cac15e9399d6a0b68 Mon Sep 17 00:00:00 2001 From: Vidar Holen Date: Sat, 4 Sep 2021 17:12:23 -0700 Subject: [PATCH] Created SC2311 (markdown) --- SC2311.md | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 SC2311.md diff --git a/SC2311.md b/SC2311.md new file mode 100644 index 0000000..5f5f87f --- /dev/null +++ b/SC2311.md @@ -0,0 +1,55 @@ +## Bash implicitly disabled set -e for this function invocation because it's inside a command substitution. Add set -e; before it or enable inherit_errexit. + +### Problematic code: + +```sh +#!/bin/bash + +#shellcheck enable=check-set-e-suppressed +set -e + +deploy() { + make -j "$(nproc)" foo test + cp ./foo /var/www/example.com/cgi-bin + ./foo --version 2>&1 +} + +version=$(deploy) +echo "Successfully deployed $version" +``` + +### Correct code: + +```sh +#!/bin/bash + +#shellcheck enable=check-set-e-suppressed +set -e +shopt -s inherit_errexit + +deploy() { + make -j "$(nproc)" foo test + cp ./foo /var/www/example.com/cgi-bin + ./foo --version 2>&1 +} + +version=$(deploy) +echo "Successfully deployed $version" +``` +### Rationale: + +ShellCheck found a Bash function invoked in a command substitution with `set -e` enabled. + +Unlike other shells, Bash disables `set -e` in command substitution by default. This means that the function will not exit on error. + +In the problematic code, the hope was that the function (and therefore the script) would fail if the build and test suite failed. Instead, the deployment continues even if the tests fail. + +This can be fixed by either using `version=$(set -e; deploy)` or by enabling `inherit_errexit` as in the correct example. + +### Exceptions: + +If you don't care that the function runs without `set -e`, you can [[ignore]] this warning. + +### Related resources: + +* Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc! \ No newline at end of file