Created
September 5, 2012 18:24
-
-
Save mildmojo/3641946 to your computer and use it in GitHub Desktop.
Getting the exit status from the command at the head of a pipeline in dash vs. bash
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DASH | |
---- | |
Not actually supported by POSIX alone. Googled from http://www.spinics.net/lists/dash/msg00327.html: | |
$ exec 3>&1 # duplicate original stdout | |
$ result=$( | |
exec 4>&1 >&3 3>&- # move cmd subst stdout, and restore original | |
{ ./main.sh; echo $? >&4 # run command, and record its status | |
} | head -n 3) | |
$ echo $result # status from ./main.sh | |
$ exec 3>&- | |
BASH | |
---- | |
$ ./main.sh | head -n 3 | |
$ echo ${PIPESTATUS[0]} # nice bash extension |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
whoa