Operators in the shell

Source: Internet
Author: User
Tags arithmetic arithmetic operators bitwise bitwise operators


Basic arithmetic operations, as well as bitwise operations, can also be implemented in the shell.

We have summed up the following points:

1) Basic arithmetic operators: + 、-、 *,/, * * are supported, the last * * is a power operation;

2) arithmetic operations must be combined with some commands to use, such as expr ' ... ' Command, $ ((...)) Command, $[...] command, let command, note: Expr is followed by an anti-quote;

3) Be sure to note that there are spaces on both sides of the operator, such as result= ' expr 2 + 3 ' is correct, if it is result= ' expr 2+3 ' is wrong! The rest of the operators also need spaces separated!!

4) The parentheses operation in the arithmetic formula, multiplication operation must add the transfer character Slash, namely result= ' expr \ (2 + 3 \) \* 5 ' is correct, the division operation does not need;

5) expr can not be a power operation;

#-----------------------------/chapter4/ex4-28.sh------------------#! /bin/sh# calculates the difference between 2 and 100, that is, -98result= ' expr 2-100 ' echo ' $result ' #计算2和100的和, i.e. 102result= ' expr 2 + ' echo ' $result ' #计算2和5的乘积, That is, 10, multiplication sign must add the transfer character backslash result= ' expr 2 \* 5 ' echo "$result" #计算24和8的商, namely 3result= ' expr 24/8 ' echo ' $result ' #先计算2和6的差, and then multiplied by 12, i.e.-48 , brackets must be added with the transfer backslash result= ' expr \ (2-6 \) \* ' echo ' $result ' #错误的语法result = ' expr 2+5 ' echo ' $result #错误的语法result = ' expr 2-4*9 ' E Cho "$result" #错误的语法result = ' expr (4-7) ' echo ' $result '

6) Use $ ((...)) You can also do algorithmic book operations, and do not need to escape the operator and parentheses! There is no need to strictly specify the operator at both ends plus a space, the space can be added without adding. as follows:

#-----------------------------/chapter4/ex4-29.sh------------------#! /bin/sh# compact format, calculated for 3 and 6 and result=$ ((3+6)) echo "$result" #松散格式, calculates 3 and 9 and result=$ ((3 + 9)) echo "$result" #计算3和6的乘积reuslt =$ ((3 *  6) echo "$result" #计算7和5的商result =$ ((7/5)) echo "$result" #计算8和3的余数result =$ ((8 3)) echo "$result" #复合运算result =$ (((1-4 ) * 5) echo "$result"

So, use $ ((...)) form is more suitable for arithmetic operations.

7) use $[...] The form and above $ ((...)) The form of the same, more commonly used;

#-----------------------------/chapter4/ex4-30.sh------------------#! /bin/sh# addition Operation Result=$[4+5]echo "$result" #复合运算result =$[(1+2) *3]echo "$result" #幂运算result =$[2 * * 4]echo "$result"

8) Let can also implement arithmetic operations. Let is followed by a direct operation expression, the variable in the expression does not need to add $, direct use, as follows:

#-----------------------------/chapter4/ex4-31.sh------------------#! /bin/sh# Define Variable n=10# addition operation let N=n+1echo "$n" #乘法运算let N=n*10echo "$n" #幂运算let N=n**2echo "$n"

9) In addition to basic operations, the shell supports arithmetic characters: + =,-=, *=,/=,%=, and bitwise operators:<<, >>, &, |, ~, ^, <<=, >>=, &=, | =, ^=, and self-increment + +, self-reduction-operation;

Operators in the shell

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.