开发者

Why does let --n have an exit code different from let n-- in bash?

开发者 https://www.devze.com 2023-02-22 12:31 出处:网络
Why do these have an exit code of 1? n=1 let --n And why does this have exit code of 0? n=1 let n-- This has exit code of 1 too... why?

Why do these have an exit code of 1?

n=1
let --n

And why does this have exit code of 0?

n=1 
let n--

This has exit code of 1 too... why?

n=1
let "n开发者_C百科 = n - 1"


man bash says:

let arg [arg ...]
          Each arg is an arithmetic expression to be evaluated (see ARITH‐
          METIC EVALUATION above).  If the last arg evaluates  to  0,  let
          returns 1; 0 is returned otherwise.

1 - 1 = 0, therefore the exit code is 1.


The difference between --n and n-- is that the first term has the value n-1 (it's decremented first and then evaluated) while the second term has the value n (post decrement). After the value is taken, n is always one less but the position of the -- says when the value should be copied into the result.

0

精彩评论

暂无评论...
验证码 换一张
取 消