You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If pipefail is enabled, the pipeline’s return status is the value of the last (rightmost) command to exit with a non-zero status, or zero if all commands exit successfully.
Variables seem to magical. Perhaps something like pipefail on or something. Besides, such option as universal would add configurability that fish shell wants to avoid.
Is not pipefail the way it works now? The $status IS the rightmost command's exit status. But in #902 , I'd like to konw the status of one certain command (in my case, it is the leftmost one). How to archive that?
pipefail means that the $status resulting from the job is the boolean AND of every job in the pipeline. So in the case of false | tee /tmp/tmp.txt, the overall status will be zero only if false and tee succeeds.
There's no proposed mechanism for getting the result of a particular command in the pipeline, but usually there's only one command in the pipeline that is likely to fail.
I don't think we should make pipefail the default. For example, the command yes | head -10 exits with code 0, but with pipefail it exits with code 141 (due to SIGPIPE). More generally, tools like head that consume part of their input and then exit would report failure if the program providing the input tries to write any more data. This includes less (which doesn't read all its input if you quit early) and other pagers.
Since this behavior is something that you'd really only want to use on specific pipelines, it seems like we should instead just have a syntax to request that a particular pipeline should evaluate to the and of the component jobs (instead of the default or). Some variation on the normal pipe operator (|). If we used && then I'd suggest |&& but we don't use that. I'm actually tempted to say we should just support foo | and bar for this behavior.
I also like the foo | and bar syntax, feels most natural to me. It kinda mirrors foo; and bar but also pipes the output from foo to bar. Then foo | bar would be the same as foo | or bar.
To me, the foo | and bar syntax implies that bar will only be run if foo exits with status 0. Sort of similar to foo | ifne bar. This is different than pipefail behavior where all commands are run in a pipeline, possibly simultaneously, and only the exit status of the pipeline potentially changes.
Also, would you have to type it after every pipe, and what happens if you don't?
foo | and bar | and baz | and qux
# vs
foo | and bar | baz | and qux
Rather than changing the behavior of pipes with a pipefail variable, the leftmost nonzero status could just always be recorded in a new variable similar to status. I can't think of a very good name, but something like status_pipefail or status_all.
Are there any common cases except SIGPIPE where left command failing is "normal"?
I'm not sure I'd seriously propose a definition of "rightmost non-0 that didn't die from SIGPIPE" but it might just be very convenient.
Is yes | head returning non-0 actually bad? It's obviously a problem in strict mode (Strict mode options #805) but otherwise, why would I care?
OK, here is one case: find | grep -q '~$'; and echo "Backup files found.". grep -q exits as soon as it sees one match, causing SIGPIPE (unless find exits quickly, it's a race). But the purpose was to check only grep's status.
OTOH consider if ls $DIR | not grep -q '~$'; echo "Definitely no backup files."; end: if you don't have permission to read $DIR, ls errors out and in this case it'd be wrong to claim "Definitely no backup files".
So the exact purpose — test last command or all — depends on the use case.
IMHO, testing all commands (i.e. pipefail) would be a saner default (if compatibility weren't a concern).
Closing because we don't want to add a pipefail option ala bash and can't change the current behavior. Also, something like $pipestatus as discussed in issue #2039 is more flexible and a helper function could be provided to return zero if all commands in the pipeline exited successfully or the rightmost non-zero status.
Activity
ridiculousfish commentedon Jun 9, 2013
What might the syntax be for this?
aeosynth commentedon Jun 9, 2013
set -U pipefail; see #805 for related discussionsugar700 commentedon Jun 12, 2013
Variables seem to magical. Perhaps something like
pipefail onor something. Besides, such option as universal would add configurability that fish shell wants to avoid.ridiculousfish commentedon Jun 16, 2013
Any reason to not make pipefail the default and only behavior?
aeosynth commentedon Jun 16, 2013
backwards compatibility i suppose
Magicloud commentedon Jul 3, 2013
Is not pipefail the way it works now? The $status IS the rightmost command's exit status. But in #902 , I'd like to konw the status of one certain command (in my case, it is the leftmost one). How to archive that?
ridiculousfish commentedon Jul 3, 2013
pipefail means that the $status resulting from the job is the boolean AND of every job in the pipeline. So in the case of
false | tee /tmp/tmp.txt, the overall status will be zero only iffalseandteesucceeds.There's no proposed mechanism for getting the result of a particular command in the pipeline, but usually there's only one command in the pipeline that is likely to fail.
Magicloud commentedon Jul 3, 2013
I see. I misunderstood the quote in first post.
lilyball commentedon Apr 25, 2015
I don't think we should make
pipefailthe default. For example, the commandyes | head -10exits with code0, but withpipefailit exits with code141(due toSIGPIPE). More generally, tools likeheadthat consume part of their input and then exit would report failure if the program providing the input tries to write any more data. This includesless(which doesn't read all its input if you quit early) and other pagers.Since this behavior is something that you'd really only want to use on specific pipelines, it seems like we should instead just have a syntax to request that a particular pipeline should evaluate to the
andof the component jobs (instead of the defaultor). Some variation on the normal pipe operator (|). If we used&&then I'd suggest|&&but we don't use that. I'm actually tempted to say we should just supportfoo | and barfor this behavior.ridiculousfish commentedon Apr 25, 2015
Very good point @ kballard. I hate to introduce more syntax like
|&&, but thefoo | and baridea is kind of amazing!maxnordlund commentedon May 8, 2015
I also like the
foo | and barsyntax, feels most natural to me. It kinda mirrorsfoo; and barbut also pipes the output fromfootobar. Thenfoo | barwould be the same asfoo | or bar.donm commentedon Aug 25, 2015
To me, the
foo | and barsyntax implies thatbarwill only be run iffooexits with status 0. Sort of similar tofoo | ifne bar. This is different thanpipefailbehavior where all commands are run in a pipeline, possibly simultaneously, and only the exit status of the pipeline potentially changes.Also, would you have to type it after every pipe, and what happens if you don't?
Rather than changing the behavior of pipes with a
pipefailvariable, the leftmost nonzero status could just always be recorded in a new variable similar tostatus. I can't think of a very good name, but something likestatus_pipefailorstatus_all.cben commentedon Dec 21, 2015
Are there any common cases except SIGPIPE where left command failing is "normal"?
I'm not sure I'd seriously propose a definition of "rightmost non-0 that didn't die from SIGPIPE" but it might just be very convenient.
Is
yes | headreturning non-0 actually bad? It's obviously a problem in strict mode (Strict mode options #805) but otherwise, why would I care?OK, here is one case:
find | grep -q '~$'; and echo "Backup files found.". grep -q exits as soon as it sees one match, causing SIGPIPE (unless find exits quickly, it's a race). But the purpose was to check only grep's status.OTOH consider
if ls $DIR | not grep -q '~$'; echo "Definitely no backup files."; end: if you don't have permission to read $DIR, ls errors out and in this case it'd be wrong to claim "Definitely no backup files".So the exact purpose — test last command or all — depends on the use case.
IMHO, testing all commands (i.e. pipefail) would be a saner default (if compatibility weren't a concern).
krader1961 commentedon Mar 18, 2017
Closing because we don't want to add a pipefail option ala bash and can't change the current behavior. Also, something like $pipestatus as discussed in issue #2039 is more flexible and a helper function could be provided to return zero if all commands in the pipeline exited successfully or the rightmost non-zero status.