Ignoring Bash pipefail for error code 141
I handle this on a per-pipeline basis by tacking on an || if ...
statement to swallow exit code 141 but still bubbling up any other errors:
pipe | that | fails || if [[ $? -eq 141 ]]; then true; else exit $?; fi
The trap
command lets you specify a command to run when encountering a signal. To ignore a signal, pass the empty string:
trap '' PIPE
There isn't a way that I know of to do this for the whole script. It would be risky in general, since there's no way to know that a child process didn't return 141 for a different reason.
But you can do this on a per-command basis. The ||
operator suppresses any errors returned by the first command, so you can do something like:
set -e -o pipefail(cat /dev/urandom || true) | head -c 10 | base64echo 'cat exited with SIGPIPE, but we still got here!'