The Linux Shell "(())" Double parenthesis operator uses the
At the beginning of learning Inux shell scripting, for its arithmetic and logical operations. It is estimated that many friends feel more difficult to accept. When using the special variable logical operator "[]", you must ensure that there is a space between the operator and the arithmetic. Arithmetic can only use: let,expr and other commands to complete. Today's statement of the double-brace "(())" structure is an extension of the arithmetic and assignment operations in the shell.
How to use:
Grammar:
(expression 1, expression 2 ... ))
Characteristics:
1, in the double bracket structure, all expressions can be like C language, such as: a++,b--, etc.
2. In a double-brace structure, all variables may not be added: "$" symbol prefix.
3, the double parenthesis can carry on the logical operation, arithmetic
4. Double bracket structure expands the FOR,WHILE,IF condition test operation
5, support multiple expression operation, between each expression with "," separate
Usage examples:
1234567891011 |
#!/bin/sh a=1; b=2; c=3; ((a=a+1)); echo $a; a=$((a+1,b++,c++)); echo $a,$b,$c |
Operation Result:
SH testsh.sh
2
3,3,4
Multiple expressions are supported between the two-parenthesis structure, which is then supported by common C-language operators such as subtraction. If the double parenthesis Band: $, the expression value is obtained and assigned to the left variable.
- Extended logical operations
12345678910 |
#!/bin/sh a=1; b= "AB" echo $ ((A>1?8:9)); ((b!= echo "ERR2" ; (a<2) && echo "OK" |
Run result:
sh testsh.sh
9
err2
OK
- Extending Process Control statements (logical relationships)
12345678910111213141516171819202122 |
#!/bin/sh
num=100;
total=0;
for
((i=0;i<=num;i++));
do
((total+=i));
done
echo $total;
total=0;
i=0;
while
((i<=num));
do
((total+=i,i++));
done
echo $total;
if
((total>=5050));
then
echo "ok"
;
fi
|
Result of Operation:
SH testsh.sh
5050
5050
Ok
With the double parenthesis operator: [[]],[],test logical operation, already let,expr can be thrown aside.]
The Linux Shell "(())" Double parenthesis operator uses the