为什么((count ++))第一次运行会返回1个退出代码 [英] Why does ((count++)) return 1 exit code first time run

查看:81
本文介绍了为什么((count ++))第一次运行会返回1个退出代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道为什么以下指示的行返回1,而随后的((count++))执行返回0.

I have no idea why the indicated line below is returning 1 while the subsequent executions of ((count++)) are returning 0.

[me@server ~]$ count=0
[me@server ~]$ echo $?
0
[me@server ~]$ count++
-bash: count++: command not found
[me@server ~]$ (count++)
-bash: count++: command not found
[me@server ~]$ ((count++))
[me@server ~]$ echo $?
1 <------THIS WHY IS IT 1 AND NOT 0??
[me@server ~]$ ((count++))
[me@server ~]$ echo $?
0
[me@server ~]$ ((count++))
[me@server ~]$ echo $?
0
[me@server ~]$ echo $count
3

推荐答案

请参见help let页上的excerpt

如果最后一个ARG的值为0,则让我们返回1;否则,返回0.返回0 否则.

If the last ARG evaluates to 0, let returns 1; 0 is returned otherwise.

由于该操作是后递增操作,因此第一次保留0时会保留((count++)),因此返回1

Since the operation is post-increment, ((count++)), for the very first time 0 is retained, hence returning 1

请注意,由于在第一次迭代本身上将值设置为1,因此对于预增量((++count))不会发生相同的情况.

Notice, the same does not happen for pre-increment ((++count)), since the value is set to 1, on the first iteration itself.

$ unset count
$ count=0
$ echo $?
0
$ ++count
-bash: ++count: command not found
$ echo $?
127
$ ((++count))
$ echo $?
0

这篇关于为什么((count ++))第一次运行会返回1个退出代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆