Suspeito que isso seja intencional (e não apenas um bug). Em caso afirmativo, direcione-me para a documentação relevante para obter uma justificativa.
~$ i=0; ((i++)) && echo true || echo false
false
~$ i=1; ((i++)) && echo true || echo false
true
A única diferença entre as duas linhas é i=0
vs.i=1
Responder1
Isso i++
ocorre porquepós-incremento, conforme descrito em man bash
. Isso significa que o valor da expressão é ooriginalvalor de i
, não o valor incrementado.
ARITHMETIC EVALUATION
The shell allows arithmetic expressions to be evaluated, under certain circumstances (see the let and
declare builtin commands and Arithmetic Expansion). Evaluation is done in fixed-width integers with no
check for overflow, though division by 0 is trapped and flagged as an error. The operators and their prece-
dence, associativity, and values are the same as in the C language. The following list of operators is
grouped into levels of equal-precedence operators. The levels are listed in order of decreasing precedence.
id++ id--
variable post-increment and post-decrement
Para que:
i=0; ((i++)) && echo true || echo false
age como:
i=0; ((0)) && echo true || echo false
exceto que isso i
também é incrementado; e essa:
i=1; ((i++)) && echo true || echo false
age como:
i=1; ((1)) && echo true || echo false
exceto que isso i
também é incrementado.
O valor de retorno da (( ))
construção é true ( 0
) se o valor for diferente de zero e vice-versa.
Você também pode testar como funciona o operador pós-incremento:
$ i=0
$ echo $((i++))
0
$ echo $i
1
E pré-incremento para comparação:
$ i=0
$ echo $((++i))
1
$ echo $i
1
Responder2
]# i=0; ((i++)) && echo true || echo false
false
]# i=0; ((++i)) && echo true || echo false
true
O valor de 'retorno' de an ((expression))
depende do pré ou pós-fixo. E então a lógica é esta:
((expression))
The expression is evaluated according to the rules described be low under ARITHMETIC EVALUATION.
If the value of the expression is non-zero,
the return status is 0;
otherwise the return status is 1.
Isso significa que ele se tornou normal, não como um valor de retorno.