Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: broken pipe message with setopt PRINT_EXIT_VALUE



2021-05-28 19:06:00 +0200, Samuel Bancal:
[...]
> < /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c 32; echo;
> XKCxcd1QQ8otBre05qUDrvw2GFIdpYNr[1]    97774 broken pipe  tr -dc
> _A-Z-a-z-0-9 < /dev/urandom |
>        97775 done         head -c 32
[...]

Same in yes | head -n 1

In both cases, tr/yes are being killed (with a SIGPIPE) because
they're trying to write to a pipe that has no reader. If they
weren't killed they would run forever as /dev/urandom is of
infinite size and yes never terminates.

When a process is killed, it returns a non-zero (failure) exit
status to its parent.

Which is printed (or rather here the "broken pipe" message
corresponding to that death-by-SIGPIPE) because of
PRINT_EXIT_VALUE.

PRINT_EXIT_VALUE (-1)
     Print the exit value of programs with non-zero exit status.  This
     is only available at the command line in interactive shells.

Even if you ignored SIGPIPE (which in general you don't want to):

~$ (trap '' PIPE; yes) | head -n1
y
yes: standard output: Broken pipe
zsh: exit 1     ( trap '' PIPE; yes; ) |
zsh: done       head -n1

You still get a message as this time, yes exit with a failure
status because the write it tried to do on that pipe failed with
a "EPIPE" error.

-- 
Stephane




Messages sorted by: Reverse Date, Date, Thread, Author