Is there a way to set bash to ignore SIGPIPE errors, or is there an approach to writing an error handler that will handle all error status codes but, say, 0 and 141?
An alternative answer to the second question, which ignores the SIGPIPE termination signal from the child process (exit code 141):
cmd1 | cmd2 | cmd3 || { ec=$?; [ $ec -eq 141 ] && true || (exit $ec); }
This uses the exit
command in a subshell to preserve the original exit code from the command pipe intact, if it's not 141. Thus, it will have the intended effect if set -e
(set -o errexit
) is in effect along with set -o pipefail
.
We can use a function for cleaner code, which allows the use of return
instead of the trick of putting exit
in a subshell:
handle_pipefails() {
# ignore exit code 141 from command pipes
[ $1 -eq 141 ] && return 0
return $1
}
# then use it or test it as:
yes | head -n 1 || handle_pipefails $?
echo "ec=$?"
# then change the tested code from 141 to e.g. 999 in
# the function, and see that ec was in fact captured as
# 141, unlike the current highest voted answer which
# exits with code 1.
# An alternative, if you want to test the exit status of all commands in a pipe:
handle_pipefails2() {
# ignore exit code 141 from more complex command pipes
# - use with: cmd1 | cmd2 | cmd3 || handle_pipefails2 "${PIPESTATUS[@]}"
for x in "$@"; do
(( $x == 141 )) || { (( $x > 0 )) && return $x; }
done
return 0
}
Aside: Question Interpretation
As pointed out in the comments to @chepner's answer, interpreting the first part of the question is more difficult -- one interpretation is, "ignoring error codes from child processes generated by SIGPIPE", and the code above accomplishes that. However, causing Bash to ignore the SIGPIPE signal entirely can make a child process write forever, since it never receives a termination signal. E.g.
(yes | head -n 1; echo $?)
# y
# 141
(trap '' PIPE; yes | head -n 1; echo $?)
# must be terminated with Ctrl-C, as `yes` will write forever
pipefail
? If so, how would test the error code from the signal that gets raised, so that I cancontinue
on 0 and 141? – Untutored