Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

git_status: Fix non-null arithmetic exit status #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ptosi
Copy link

@ptosi ptosi commented Feb 19, 2020

Move from post-increment to pre-increment in the counter implementation in order to avoid the behaviour where (( X++ )) returns 1 if X evaluates to 0, which is interpreted as a failure exit
status, in turn causing the whole script to terminate prematurely.

Note that, for X non-negative, (( ++X )) never evaluates to 0, avoiding the unintended behaviour.

Reference:

$ bash -c "help '(('"

(( ... )): (( expression ))
    Evaluate arithmetic expression.

    The EXPRESSION is evaluated according to the rules for arithmetic
    evaluation.  Equivalent to "let EXPRESSION".

    Exit Status:
    Returns 1 if EXPRESSION evaluates to 0; returns 0 otherwise.

Move from post-increment to pre-increment in the counter implementation
in order to avoid the behaviour where
    (( X++ ))
returns 1 if X evaluates to 0, which is interpreted as a failure exit
status, in turn causing the whole script to terminate prematurely.

Note that, for X non-negative,
    (( ++X ))
never evaluates to 0, avoiding the unintended behaviour.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant