1)如果我操作整数,我应该使用$(())吗?
>typeset -i x=0
>typeset -i y=0
>typeset -i z=0
>y=$(($x+1))
>print $y
1
>z=$x+1
>print $z
1
Run Code Online (Sandbox Code Playgroud)
如您所见,z和y都有正确的结果.
仅在变量未声明为整数的情况下才有区别:
>typeset j
>typeset k
>j=$(($x+1))
>print $j
1
>k=$x+1
>print $k
0+1
Run Code Online (Sandbox Code Playgroud)
2)$(($ x + 1))和$((x + 1))之间有什么区别?
print $(($ x + 1))
1
print $((x + 1))
1
let有同样的情况:
x = 1
让x = $ x + 1
打印$ x
2
让x = x + 1
打印$ x
3
2) $x 在 $(()) 求值之前展开:
x=1+
echo $(($x 1))
=>2
echo $((x 1))
=>syntax error when trying to make an operand from "1+"
Run Code Online (Sandbox Code Playgroud)