Skip to content

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