Add an alternative for supporting

Trevor Gross
2024-02-20 00:53:37 -05:00
parent ae187455cb
commit 2d41c804d2

@@ -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: