Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
revisit exit code of `while` builtin #4982
I'm not sure the current exit code for
while foo if bar break end end
This loop can terminate with either a zero or non-zero exit code. But it's equal to the following, which can only terminate with a zero exit code:
while true if not bar break end end
Now if the condition failed to run (instead of ran and exited with a non-zero exit code), it would make sense for
while does-not-exist end
Here, it would make sense for
My problem is with a script or function that executes in a loop, an explicit
I like the general principle of "if it's run at least once" but I feel like "if it never runs this is unexpected behavior/an error" is too big of an assumption to make and we'll just get complaints about it later (if anyone actually cares about these things). I think it should just return 1 in case there was an error evaluating the target (i.e. target does not exist).
But it's not?
A return of 1 isn't "unexpected behavior". It's just a false return. It's something you have to deal with all the time, and which happens for quite trivial reasons, and which has no huge effect.
It's another way to convey information, not a big issue.
If the "target" does not exist, that would warrant an error message, not just a return value of 1.
Well, yes and no. An error message is for the user, but the return code is for programmatic evaluation (which I understand you already cover with the "not just" in your statement). But my point is that nothing else explicitly warrants a non-zero return code for
I understand that a non-zero return code is not code for "error occurred," but as you mention, it is intended to evaluate to
Users wishing to explicitly take an action based on the result of a condition would be best served with
Additionally, you have to consider the return code in the context of the script or function, as I initially mentioned. Given a function consisting solely of the following:
function foo if bar # do something end end
It is understandable why the function would return non-zero if the
But for a function that looks like this:
function foo bar | while read -l baz # do something end end
It really doesn't make sense for the default behavior here to be for the function return non-zero. If anything, in this case the return code of
I'm not sure that line of reasoning works. "For
I think it's reasonable to say that the point of
That's more debatable. In this specific case you might want
#2039 would introduce a $pipestatus variable, which would allow you to pick after the fact.
I don't think "always" returning 0 is awful, by any means. It's better than "basically always" returning non-zero.
But I think adding that extra behavior of "failing" when the loop is never entered adds an extra bit of information that is quite hard to retrieve otherwise.
Alternatively, it'd be possible to introduce