operator of the Shell

Source: Internet
Author: User
Tags arithmetic
an arithmetic operator:

+-*/% (take more) * * (Power) 1.1 expr evaluates an expression value

Expr expression (expr cannot calculate a power operation)

Example:

#。 /bin/bash

#计算2和100的差, i.e.-98

result= ' Expr 2-100 '

echo "$result"

#计算2和100的和, which is 102

result= ' Expr 2 +100 '

echo "$result"

#计算2和5的乘积, which is 10

result= ' Expr 2 \*5 '

echo "$result"

#计算24和8的商, which is 3

result= ' Expr 24/8 '

echo "$result"

#先计算2和6的差, then multiply by 12, i.e.-48

result= ' expr \ (2-6 \) \* 12 '

echo "$result"

#错误的语法, no spaces

result= ' Expr 2+5 '

echo "$result"

#错误的语法

result= ' expr2-4*9 '

echo "$result"

#错误的语法

Result= ' expr1-(4-7) '

echo "$result" 1.2 is calculated with $ (()) :

#! /bin/sh

#紧凑格式, calculates the and of 3 and 6

result=$ ((3+6))

echo "$result"

#松散格式, calculates the and of 3 and 9

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"

1.3 calculated using $[]

#! /bin/sh

#加法运算

RESULT=$[4+5]

echo "$result"

#复合运算

result=$[(1+2)

echo "$result"

#幂运算

result=$[2 * * 4]

echo "$result"

1.4 calculation with let command

The Let command executes one or more arithmetic expressions, and the variable name does not need to be $, and if the expression has spaces or other special characters, it is referenced.

#! /bin/sh

#定义变量

n=10

#加法运算

Let N=n+1

echo "$n"

#乘法运算

Let n=n*10

echo "$n"

#幂运算

Let N=n**2

echo "$n" two-bit operator

<< left Shift

>> Right Shift

& Bitwise AND

| Bit or

~ Bit non-

^ bit XOR or 2.1 self-increment decrement operator:

++varibale first add 1, and then assign to variable

--variable first minus 1, then to variable.

vaiable++ first with variable, plus 1.

variable--first with variable, and then minus 1 2.2 numeric constants in the system:

By default, the shell represents a number in decimal, and it can be binary, octal, and hexadecimal.

Added prefix: 0 means 8 hexadecimal, 0x

The well number #:2#100 represents 2 binary, and 8#42 represents 8 binary

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.