linux - (loopcount=loopcount+1) 和 ((loopcount=loopcount+1)) 有什么区别?

标签 linux shell while-loop

loopcount=1
loopmax=5
while [ $loopcount -le $loopmax ]
do
echo "loop iteration :$loopcount"
((loopcount=loopcount+1))
done

为此我得到了这样的 o/p

loop iteration :1
loop iteration :2
loop iteration :3
loop iteration :4
loop iteration :5

但是如果我将程序 ((loopcount=loopcount+1)) 更改为 (loopcount=loopcount+1) 我会得到以下输出。

loop iteration :1
loop iteration :1
loop iteration :1
loop iteration :1
loop iteration :1
loop iteration :1

获得无限次。 () 和 (()) 有什么区别?

最佳答案

(...) 表示在子 shell 中运行给定的命令。 ((...))表示在括号内进行算术运算。

请注意,子 shell 无法更改父 shell 的变量,因此在您的示例中,您永远不会更新父 loopcount 的值。此外,在您的单亲示例中,您不会进行算术运算,而是将字符串 loopcount+1 分配给变量 loopcount 这样如果您执行了 printf "%s\n""$loopcount" 之后你会得到输出 loopcount+1

关于linux - (loopcount=loopcount+1) 和 ((loopcount=loopcount+1)) 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35410464/

相关文章:

bash - 为什么在没有为测试提供任何参数时,在 bash 中使用 -f 进行测试会返回 true?

python - tkinter root.mainloop 与 While True 循环

c - 为什么 while 循环会在我的代码中产生错误?

regex - 如何在 te gnome-terminal 中使用 sed 命令实现语法高亮?

shell - 想要在shell脚本中解析一个字符串

c - 在 linux 上的 C 中,你将如何实现 cp

linux - 安装后无法访问phpmyadmin

c - while (getchar () != '\n' );

c - Linux编程: Writing to duplicated socket fails

java - 在 Linux 上更改 Netbeans 的光标