The following points are the difference between ( )and (()) I understand. I the don't know the mechanism inside them, but at least, It's the
Behavior of them.
(1) () : will open a new nested shell to excute it. for example:
Enter the following statements in script file:
pwd
(cd /var/tmp;pwd)
pwd
cd /var
pwd
The results are shown as below:
/export/home/appadm/jjx
/var/tmp
/export/home/appadm/jjx
#Because that the cd command was executed in a nested shell, all the environment variable changed in nested shell will not affect the parent shell. so when the nested shell quits out, the results of first two pwd command in parent shell are same.
/var
(2) (()): we can do mathematical calculation in this operator.
In (()), all nonnumeric characters are took as variable. For example:
#!/usr/bin/ksh
a=1
b=2
((a = a+b))
In this statement, a and b are variable. and the result of the statement is 3. That is, in (()), we need not to refer variable by add prefix $.
however, the statement ((a=$a+$b)) is also correct.
PLS pay attention:
a=$a+$b is not a mathematical calculation. It only joint three characters and replace $a and $b with the actual value of them.
So, the result of "a=$a+$b" is "1+3".
本文详细解析了Shell脚本中()与(()))的不同之处:()用于创建子shell并执行命令,子shell中的环境变量更改不会影响父shell;(()))则专门用于数学运算,其中所有非数字字符被视为变量。

被折叠的 条评论
为什么被折叠?



