Skip to content

Instantly share code, notes, and snippets.

@mildmojo
Created September 5, 2012 18:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mildmojo/3641946 to your computer and use it in GitHub Desktop.
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
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
@nuex
Copy link

nuex commented Sep 5, 2012

whoa

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment