Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 12 Dec 2017 11:48:20 +0000
From:      bugzilla-noreply@freebsd.org
To:        freebsd-bugs@FreeBSD.org
Subject:   [Bug 224270] Get exit status of process that's piped to another: set -o pipefail is missing for /bin/sh
Message-ID:  <bug-224270-8@https.bugs.freebsd.org/bugzilla/>

Next in thread | Raw E-Mail | Index | Archive | Help
https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=3D224270

            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@FreeBSD.org
          Reporter: wosch@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 comm=
and,
unless
       the pipefail option is enabled.  If pipefail is enabled, the pipelin=
e's=20
return
       status  is  the  value  of the last (rightmost) command to exit with=
 a
non-zero
       status, or zero if all commands exit successfully.=20

$ 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=E2=80=99t check the pipe status, a=
s described
in
https://unix.stackexchange.com/questions/14270/get-exit-status-of-process-t=
hats-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.

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



Want to link to this message? Use this URL: <http://docs.FreeBSD.org/cgi/mid.cgi?bug-224270-8>