The Linux Shell "(())" Double parenthesis operator uses the

Source: Internet
Author: User
Tags arithmetic

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:

    • Extended arithmetic
1234567891011 #!/bin/sha=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/shnum=100;total=0;for((i=0;i<=num;i++));do    ((total+=i));doneecho $total;total=0;i=0;while((i<=num));do    ((total+=i,i++));doneecho $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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.