From 2d41c804d2f9715ef7a7c15e3ae10abc855105cc Mon Sep 17 00:00:00 2001 From: Trevor Gross Date: Tue, 20 Feb 2024 00:53:37 -0500 Subject: [PATCH] Add an alternative for supporting --- SC3040.md | 8 ++++++++ 1 file changed, 8 insertions(+) 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: