尝试一下:
$ a=0
$ let a++
$ echo $?
1 # Did the world just go mad?
$ echo $a
1 # Yes, it did.
$ let a++
$ echo $?
0 # We have normality.
$ echo $a
2
与此对比:
$ b=0
$ let b+=1
$ echo $?
0
还有这个(来自西雷克斯):
$ c=0
$ let ++c
$ echo $?
0
这里发生了什么?
$ bash --version
GNU bash, version 4.1.5(1)-release (x86_64-pc-linux-gnu)
答案1
从help let
:
Exit Status:
If the last ARG evaluates to 0, let returns 1; let returns 0 otherwise..
既然var++
是邮政-increment,我猜是最后一个参数做评估为零。微妙的...
也许更清楚的说明:
$ let x=-1 ; echo x=$x \$?=$?
x=-1 $?=0
$ let x=0 ; echo x=$x \$?=$?
x=0 $?=1
$ let x=1 ; echo x=$x \$?=$?
x=1 $?=0
$ let x=2 ; echo x=$x \$?=$?
x=2 $?=0