Skip to content

Add $pipestatus variable #2039

Closed
Closed

Description

@lilyball

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:

  • $status and $status[1] would not be equivalent. This might be mildly surprising, because for any single-element variable, $foo and $foo[1] are equivalent, and $status appears 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:

    • ! cmd puts the return value of cmd in $PIPESTATUS and its negation in $?
    • if cond; then body; fi, if cond returns non-zero, puts the return value in $PIPESTATUS but puts 0 in $?. If cond returns zero then $? and $PIPESTATUS always agree. If cond returns non-zero but there's an else block then $? and $PIPESTATUS always agree unless the else block consists of a command that doesn't alter the exit status (for example case x in esac)
    • while cond; do body; done puts the last exit status of the cond in $PIPESTATUS but puts 0 in $?. It only places 0 in $PIPESTATUS if it was broken with break.
    • until ! cond; do body; done behaves like while cond does
    • for loops (both forms) always preserve the existing status when evaluating the condition. So a for loop that evaluates the body at least once will evaluate to the status of the body (for both $? and $PIPESTATUS). But a for loop that iterates zero times will put the previous status in $PIPESTATUS but put 0 in $?.
    • case where none of the patterns matches preserves the existing status and puts it in $PIPESTATUS, but puts 0 in $? (just like a for loop with zero iterations). A degenerate example is case 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 to 0 after evaluating their condition (whether or not the condition itself affects the pipe status, e.g. case statements and for loops 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 example (exit 3); case x in esac has a $? of 0 but puts 3 in $PIPESTATUS). Update: It's actually just not modifying $PIPESTATUS at all in that case, rather than preserving a single "pipe status" value. (exit 3) | (exit 5); case x in esac sets $? to 0 but leaves ${PIPESTATUS[@]} at 3 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

  1. added this to the next-major milestone on Apr 26, 2015
  2. ridiculousfish commented on Apr 26, 2015

    @ridiculousfish
    Member

    Thanks for this detailed writeup @kballard

  3. anordal commented on Apr 1, 2016

    @anordal
    Contributor

    For the purpose of #2791, I had a suggestion about $stmt_status as a (hopefully) more general concept.

  4. floam commented on Apr 3, 2016

    @floam
    Member

    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):

    screenshot 2016-04-03 at 8 38 24 am

    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.

  5. ridiculousfish commented on Apr 4, 2016

    @ridiculousfish
    Member

    Too cool!

  6. nyarly commented on Jul 14, 2016

    @nyarly
    Contributor

    Returning to this problem a couple of times, I keep wondering if the psub analogy isn't a good one.

    I.e., instead of

    a | b | c | d
    # logic here to figure out what failed...
    if not test $statuses[1]
      echo "a failed"
    end
    #...
    
    set -l out (mktemp)
    if not a > $out
      echo "a failed"
    end
    set -l out (mktemp)
    if not b > $out
      echo "b failed"
    end
    #...
    

    I guess there's something more complicated using mkfifo and backgrounding and job management...

  7. faho commented on Jul 14, 2016

    @faho
    Member

    @nyarly: All those temp files are going to hurt.

  8. nyarly commented on Jul 14, 2016

    @nyarly
    Contributor

    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

    while jobs | grep -q running
      sleep 0.1
    end
    

    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.

  9. Qix- commented on Jan 14, 2019

    @Qix-

    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.

    image

    Really missing something like this in Fish.

  10. zabereer commented on Feb 9, 2019

    @zabereer
    Contributor

    I took the liberty of adding $pipestatus support, 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.

  11. zanchey commented on Feb 25, 2019

    @zanchey
    Member

    Fixed with #5632.

  12. locked as resolved and limited conversation to collaborators on Apr 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions