[Bug 224270] Get exit status of process that's piped to another: set -o pipefail is missing for /bin/sh

bugzilla-noreply at freebsd.org bugzilla-noreply at freebsd.org
Tue Dec 12 11:48:20 UTC 2017


https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=224270

            Bug ID: 224270
           Summary: Get exit status of process that's piped to another:
                    set -o pipefail is missing for /bin/sh
           Product: Base System
           Version: CURRENT
          Hardware: Any
                OS: Any
            Status: New
          Severity: Affects Only Me
          Priority: ---
         Component: bin
          Assignee: freebsd-bugs at FreeBSD.org
          Reporter: wosch at FreeBSD.org

The bash, zsh and ksh have a nice feature to detect a non-zero status in a
pipe:
from the bash(1) manual page:

       The  return status of a pipeline is the exit status of the last command,
unless
       the pipefail option is enabled.  If pipefail is enabled, the pipeline's 
return
       status  is  the  value  of the last (rightmost) command to exit with a
non-zero
       status, or zero if all commands exit successfully. 

$ bash -c 'set -o pipefail; false | true; echo $?'
1
$ ksh93 -c 'set -o pipefail; false | true; echo $?'
1
$ zsh -c 'set -o pipefail; false | true; echo $?'
1

However, this feature is missing in /bin/sh:

$ sh -c 'set -o pipefail; false | true; echo $?'
set: Illegal option -o pipefail


$ sh -c 'false | true; echo $?'
0

There are workaround for shell which don’t check the pipe status, as described
in
https://unix.stackexchange.com/questions/14270/get-exit-status-of-process-thats-piped-to-another

(((((exec 3>&- 4>&-; someprog); echo $? >&3) | filter >&4) 3>&1) | (read xs;
exit $xs)) 4>&1

but this looks bizarre and you have to do it for every command in the pipe
line.

I really wish that we have the 'set -o pipefail' option setting in FreeBSD
/bin/sh too.

-- 
You are receiving this mail because:
You are the assignee for the bug.


More information about the freebsd-bugs mailing list