Skip to content

revisit exit code of while builtin #4982

@mqudsi

Description

@mqudsi

I'm not sure the current exit code for while when the loop evaluation condition returns non-zero makes sense. I understand that it passes through the value returned by the condition executable, but I'm not sure that a non-zero exit code accurately reflects the intent of the code, nor is particularly useful.

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 to return non-zero:

while does-not-exist
end

Here, it would make sense for $status to evaluate to non-zero after the loop.

My problem is with a script or function that executes in a loop, an explicit return/exit 0 is needed to "override" the misleading nonzero exit code after the loop, which shouldn't (imho) ever be necessary unless a failure was detected and is being explicitly ignored.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions