Repository navigation
Description
We don't currently have any way to retrieve the exit codes from the independent jobs in a given pipeline. Bash has $PIPESTATUS. I propose that Fish should add $pipestatus. This is backwards-incompatible because it will break anyone currently using that variable name, but I don't know how much code (if any) will actually be affected.
Also see #763 (add pipefail).
An alternative to this would be to define a new class of variable which can be treated as either an array or as a scalar. Right now an array variable expands to all values when referenced. We could add a variant where the variable expands to one value when referenced by itself, but can be indexed or sliced like an array. We could then adjust the $status variable to behave like this; when referenced as just $status it expands to the status of the pipeline as a whole, but it can be referenced as $status[1..-1] to expand to the statuses of each job in the pipeline (where $status[1] is the first job and $status[-1] is the last job).
A couple of quirks with this suggestion that would have to be considered:
-
$statusand$status[1]would not be equivalent. This might be mildly surprising, because for any single-element variable,$fooand$foo[1]are equivalent, and$statusappears to be a single-element variable, but since this equivalence doesn't hold for any multi-element variable I think that's ok. -
In Bash, it's possible for
$?to contain a value that isn't found in${PIPESTATUS[@]}. This can happen when using the!negation operator or when using a control flow construct. Experimentally, I've observed the following behavior:! cmdputs the return value ofcmdin$PIPESTATUSand its negation in$?if cond; then body; fi, ifcondreturns non-zero, puts the return value in$PIPESTATUSbut puts0in$?. Ifcondreturns zero then$?and$PIPESTATUSalways agree. Ifcondreturns non-zero but there's anelseblock then$?and$PIPESTATUSalways agree unless theelseblock consists of a command that doesn't alter the exit status (for examplecase x in esac)while cond; do body; doneputs the last exit status of thecondin$PIPESTATUSbut puts0in$?. It only places0in$PIPESTATUSif it was broken withbreak.until ! cond; do body; donebehaves likewhile conddoesforloops (both forms) always preserve the existing status when evaluating the condition. So aforloop that evaluates the body at least once will evaluate to the status of the body (for both$?and$PIPESTATUS). But aforloop that iterates zero times will put the previous status in$PIPESTATUSbut put0in$?.casewhere none of the patterns matches preserves the existing status and puts it in$PIPESTATUS, but puts0in$?(just like aforloop with zero iterations). A degenerate example iscase x in esac.
Overall, it seems as though Bash maintains a separate "pipe status" value distinct from
$?. Most commands set both, but certain constructs only affect$?. Specifically,!only affects$?, and all control flow constructs reset$?back to0after evaluating their condition (whether or not the condition itself affects the pipe status, e.g.casestatements andforloops do not). Also of note, variable assignments involving command substitutions (foo=$(bar)) propagate the command substitution's exit status to its own exit status but other command substitutions do not.Furthermore, the "pipe status" value is preserved across pipelines if the pipeline consists of commands that do not alter the exit status (for exampleUpdate: It's actually just not modifying(exit 3); case x in esachas a$?of0but puts3in$PIPESTATUS).$PIPESTATUSat all in that case, rather than preserving a single "pipe status" value.(exit 3) | (exit 5); case x in esacsets$?to0but leaves${PIPESTATUS[@]}at3 5.Given all that, it's unclear how Fish should behave. I'm tempted to say that it should just do the simple thing and use the exit status of each job as reported by
$status, but I'm not sure why Bash behaves the way it does; maybe the ability to retrieve the "real" statuses from previous commands is useful in some cases?
Activity
ridiculousfish commentedon Apr 26, 2015
Thanks for this detailed writeup @kballard
anordal commentedon Apr 1, 2016
For the purpose of #2791, I had a suggestion about
$stmt_statusas a (hopefully) more general concept.floam commentedon Apr 3, 2016
Check out what elvish does! (this is a strange bug, also, that most shells aren't handling well. Elvish appears to be very unique in crashing the "right way" (see the reddit thread for details. for me it's broken on everything I tried: zsh, bash, fish, csh, and mksk, where I could on OS X, Linux, and NetBSD), which is impressive, but I'm referring to just pipe error status handling and display in general):
It requires no explanation from me as to what you're looking at - it's wonderfully intuitive for a modality I've never experienced before trying that today at an unfamiliar shell.
ridiculousfish commentedon Apr 4, 2016
Too cool!
nyarly commentedon Jul 14, 2016
Returning to this problem a couple of times, I keep wondering if the psub analogy isn't a good one.
I.e., instead of
I guess there's something more complicated using
mkfifoand backgrounding and job management...faho commentedon Jul 14, 2016
@nyarly: All those temp files are going to hurt.
nyarly commentedon Jul 14, 2016
I was looking at psub, and something like it's "when the caller exits, rm these" seems like it'd handle things. I almost want a generic "cleanup this file" now.
Ultimately, I wound up using mkfifo, which was mostly okay. The big hassle was
So, #1422 would help there. There's also the challenge of making sure that the fifos drain properly.
I'm intrigued by this approach, and am kind of wondering about building a generic function to handle fifo chains.
Qix- commentedon Jan 14, 2019
Having elvish-like output for fish would be incredibly helpful. Currently, my zsh has something slightly similar - airline shows the exit codes in a line.
Really missing something like this in Fish.
zabereer commentedon Feb 9, 2019
I took the liberty of adding
$pipestatussupport, I hope I did it right.I use it heavily every day for build commands on zsh (probably a bad habit but miss it on fish):
make something | grep something_else ; exit $pipestatus[1].And now I am switching to fish!! So pleased.
zanchey commentedon Feb 25, 2019
Fixed with #5632.