The difference between (()) and ().

The following points are the difference between ( )and (()) I understand. I the dont know the mechanism inside them, but at least, Its 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".

经验分享 程序员 微信小程序 职场和发展