There are four ways in which the Bash Shell's arithmetic operations

Source: Internet
Author: User
Tags arithmetic


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1: Using expr external program
Addition R= ' Expr 4 + 5 '
Echo $r
Attention! ' 4 ' + ' 5 ' There should be a gap between the three
R= ' Expr 4 * 5 ' #错误
Multiplication r= ' expr 4 \* 5 '
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2: Use $ (())
r=$ ((4 + 5))
Echo $r
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3: Use $[]
R=$[4 + 5]
Echo $r
Multiplication
R= ' Expr 4 \* 5 '
r=$ ((4 * 5))
r=$[4 * 5]
Echo $r
Division
r= ' Expr 40/5 '
r=$ ((40/5))
r=$[40/5]
Echo $r
Subtraction
r= ' Expr 40-5 '
r=$ ((40-5))
r=$[40-5]
Echo $r
Find remainder
r=$[100% 43]
Echo $r
(e.g. 2 of 3)
r=$ ((2 * * 3))
r=$[2 * * 3]
Echo $r
Note: Expr does not have a
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4: Use the Let command
Addition:
n=10
Let N=n+1
echo $n #n =11
Multiplication:
Let m=n*10
Echo $m
Division:
Let R=M/10
Echo $r

To find the remainder:
Let R=m%7
Echo $r

By emerges:
Let R=m**2
Echo $r

Although the bash shell has four arithmetic operations, not each of which is cross-platform, it is recommended that expr be used.
In addition, we often have 1 operations in script, the following four methods are available:
m=$[m + 1]
m= ' expr $m + 1 '
m=$ (($m + 1))
Let M=m+1

There are four ways in which the Bash Shell's arithmetic operations

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.