Skip to content

Files

Latest commit

 

History

History
54 lines (36 loc) · 1.3 KB

SC2311.md

File metadata and controls

54 lines (36 loc) · 1.3 KB

Pattern: Use of command substitution with set -e enabled

Issue: -

Description

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.

Example of incorrect code:

#!/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"

Example of correct code:

#!/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"

Further Reading