Blog 2025/5/8
One of the problems with using bash's set -o pipefail
is that a command may fail with SIGPIPE
if the next command in the pipepine exits before receiving all of its input.
For example:
command_which_creates_lots_of_output | grep -q foo
Here, grep will exit as soon as it encounters the first instance of "foo" on stdin.
If command_which_creates_lots_of_output
still has more output to write,
it will receive SIGPIPE
, causing the pipeline to fail.
We can work around this by writing a C program which copies stdin to stdout but ignores SIGPIPE.
Compiling:
gcc -std=c99 -Wall -Werror -o nosigpipe nosigpipe.c
Revisiting our above example:
command_which_creates_lots_of_output | ./nosigpipe | grep -q foo