bash – 为什么((count))在第一次运行时返回1个退出代码

前端之家收集整理的这篇文章主要介绍了bash – 为什么((count))在第一次运行时返回1个退出代码前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我不知道为什么下面指示的行返回1而((count))的后续执行返回0.
  1. [me@server ~]$count=0
  2. [me@server ~]$echo $?
  3. 0
  4. [me@server ~]$count++
  5. -bash: count++: command not found
  6. [me@server ~]$(count++)
  7. -bash: count++: command not found
  8. [me@server ~]$((count++))
  9. [me@server ~]$echo $?
  10. 1 <------THIS WHY IS IT 1 AND NOT 0??
  11. [me@server ~]$((count++))
  12. [me@server ~]$echo $?
  13. 0
  14. [me@server ~]$((count++))
  15. [me@server ~]$echo $?
  16. 0
  17. [me@server ~]$echo $count
  18. 3
请参阅帮助允许页面中的摘录,

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

由于操作是后递增的,((计数)),第一次保留0,因此返回1

注意,对于预增量((count))也不会发生同样的情况,因为在第一次迭代时,该值设置为1.

  1. $unset count
  2. $count=0
  3. $echo $?
  4. 0
  5. $++count
  6. -bash: ++count: command not found
  7. $echo $?
  8. 127
  9. $((++count))
  10. $echo $?
  11. 0

猜你在找的Bash相关文章