diff --git a/SC3040.md b/SC3040.md index 02b5adc..b192df3 100644 --- a/SC3040.md +++ b/SC3040.md @@ -44,6 +44,14 @@ If the file has been written to when the pipeline is done, it means one of the c This can obviously be extended with e.g. `echo "cmd1=$?" >> file` if the particular exit codes for particular commands are desired. +If `pipefail` or other options are used only as a sanity check, another option is to try the setting in a subshell and only apply it in the main shell if successful. This will set the flag on platforms where `sh` is linked to `ash`, `busybox`, or `bash`, but do nothing if `sh` is `dash`. + +```sh +# Set pipefail if it works in a subshell, disregard if unsupported +# shellcheck disable=SC3040 +(set -o pipefail 2> /dev/null) && set -o pipefail +``` + Alternatively, switch to a shell that supports `pipefail`, such as Bash. ### Related resources: