Four arithmetic operations of the Linux shell

Source: Internet
Author: User
Tags arithmetic

There are several ways to do this in Bash's arithmetic operations:
Example of name syntax
Arithmetic extension $ ((arithmetic)) r=$ ((1+2*3))
Use external program expr expr arithmetic r= ' expr 1+2*3 '
Use $[] $[arithmetic] r=$[1+2]
Using built-in commands declare-i variable = arithmetic declare-i r=1+2*3
Use built-in commands
let-let arithmetic let r=1+2

minus 1 after i++ operation Plus 1,i--operation
++i operation before adding 1,--i operation minus 1

first, arithmetic expansion
The syntax for an arithmetic extension is: $ ((expression)), if there is a variable in the expression, it is better not to add a $ this symbol before the variable, so that the variable does not exist to cause a syntax error, for example:
unset I
echo $ (($i))
Because the variable i does not exist, it becomes echo $ (()), so the syntax is wrong.
However, if it is written as Echo $ ((2+i)), ((2+i)) is a valid arithmetic, even if the variable does not exist, can still be calculated correctly.

A=5
a=$ ((++1)) A has a value of 1 and is assigned to a
The value of a=$ ((--1)) A is reduced by 1 and is assigned to a
a=$ ((3+a--)) operation minus 1
second, using the external program expr to do arithmetic operations
An external program, expr, is intended to display the value of an expression in "standard output". This is an external program, and the shell is not related, so the portability is very good,
If you focus on cross-platform, you can use expr instead of other arithmetic expressions on your feet.

Expr 3 + 4
It will display 7 on the screen, note that there are spaces on both sides of the "+".
When using expr, the special "expression" contains the special characters of the shell, such as *, |, <, > 、!、 &, (,), to be escaped with "\".
1. r= ' expr parameter 1 \| parameter 2 '
The meaning of "or". If parameter 1 exists, is not empty, is not 0, returns the value of parameter 1, or returns the value of parameter 2.
R= ' Expr 3 \| The value of 0 ' R is 3
R= ' Expr 0 \| The value of 2 ' R is 2
2. r= ' expr parameter 1 \& parameter 2 '
if both "parameter 1" and "Parameter 2" are present, non-empty, not 0, return the value of parameter 1, or return 0.
R= ' expr 3 \& 0 ' R has a value of 0
3. Comparison
Whether arithmetic is a value of R
r= ' expr 1 \< 2 ' is 1
R= ' Expr 3 \<= 2 ' no 0
R= ' Expr 2 \= 2 ' is 1
R= ' Expr 3 \!= 2 ' is 1
R= ' Expr 3 \>= 2 ' is 1
R= ' Expr 3 \> 2 ' is 1
4, plus
R= ' Expr 4 + 5 ' r has a value of 9
5, reduce
r= ' expr 3-5 ' r has a value of-2
6. Multiply
R= ' Expr 3 \* 5 ' r has a value of 15
7, in addition to
r= ' expr 32/5 ' r has a value of 6 (remainder unconditional)
8. The exponentiation
Expr has no power to function
9. Seeking remainder
r= ' Expr 5 ' r has a value of 2
10. Calculate String Length
r= ' expr length ' hello ' r ' value is 5
third, using $[] to do arithmetic operations
Use $[] to do arithmetic operations and $ (()) Similar
Plus: R=$[4+5] R has a value of 9
Minus: R=$[4-5] R has a value of-1
Multiply by: R=$[4*5] R has a value of 20
Except: R=$[5/2] R has a value of 2
Remainder: r=$[5%2] R has a value of 1
Powers: R=$[2**3] R has a value of 8

Iv. using declare and let to do arithmetic operations
declare is a built-in command for bash shell.
Declare-i I
I=5+4
Echo $I
Because I was previously defined as an integer (arithmetic operation), the value of $i is 9, and is no longer a string 5+4
After being defined as an integer, you can add, subtract, multiply, and divide. and other operations
v. Doing arithmetic operations with let

Let is the built-in command for bash shell.

Let I=8+4*5
Echo $I
The value of the output at this time is 28
Let shows an arithmetic that is directly behind.
You can also use a space character to make the expression more readable, but you must use quotation marks to include an expression.
Let "i = i + 5"

This article is from the "Welcome to Linux World" blog, so be sure to keep this source http://linuxnote.blog.51cto.com/9876511/1641189

Four arithmetic operations of the Linux 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.