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

Source: Internet
Author: User
Tags arithmetic

Guide 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: Syntax:
(expression 1, expression 2 ... ))
Features:
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, the double bracket structure expands the for,while,if condition to test the operation 5, supports the multiple expression operation, each expression uses "," separates
usage Examples: Extended Arithmetic
#!/bin/sh a=1;b=2;c=3; ((a=a+1)); Echo $a; a=$ (a+1,b++,c++); Echo $a, $b, $c
Running result:]# sh testsh.sh23,3,4 supports multiple expressions between the two-bracket structures, and then the C-language common operators such as subtraction are supported. If the double parenthesis Band: $, the expression value is obtained and assigned to the left variable.
Extended Logical Operations
#!/bin/sh a=1;b= "AB"; echo $ ((a>1?8:9)); ((b!= "a")) && echo "ERR2";((a<2)) && echo "OK";
Operating result:]# sh testsh.sh9err2ok
Extending Process Control statements (logical relationships)
#!/bin/sh num=100;total=0; For ((i=0;i<=num;i++));d o    ((total+=i));d Oneecho $total, Total=0;i=0;while ((i=5050)); then    echo "OK"; fi
Operation Result:]# SH testsh.sh50505050ok

With the double parenthesis operator: [[]],[],test logical operation, already let,expr can be thrown aside.]

Free to provide the latest Linux technology tutorials Books, for open-source technology enthusiasts to do more and better: http://www.linuxprobe.com/

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.