Shell Getting Started Tutorial: arithmetic operations

Source: Internet
Author: User
Tags arithmetic



Bash's arithmetic operations are available in the following ways:


Serial Number name Grammar Example
1 Arithmetic extension $ (arithmetic type) r=$ ((2+5*8))
2 Using external programs Expr arithmetic type R= ' Expr 4 + 5 '
3 Use $[] $[Arithmetic Type] R=$[4+5]
4 Using built-in commands declare Declare-i variable = formula Declare-i r=8+16
5 Using the built-in command let Let arithmetic type Let r=8+16





Section One: Arithmetic expansion





Arithmetic extension syntax: $ (arithmetic)


If there is a variable in the expression, it is best not to add $ to this symbol before the variable name, so that the syntax error is caused when the variable does not exist.



For example:


unset i
echo $((12+$i))


Because the variable i does not exist (line 1), line 2 becomes: Echo $ ((12+)), so the syntax is wrong.



But if you write:


echo $ ((12+i))


This is done correctly because 12+i is a valid arithmetic expression in $ (()) and can still be evaluated correctly even if the variable i does not exist (the result is 12).



If there is a "variable extension" in an expression, add $, for example: Echo $ ((${j:-8}+2)).



Here's an enhanced impression:


$ ((12+i)) Correct example
$ ((12+ $i)) Syntax error when I does not exist, not recommended
$ ((12+${i})) Syntax error when I does not exist, not recommended
$ ((${j:-8}+2)) The correct example of a "variable extension" in an expression


Here are a variety of simple arithmetic uses:



1, plus r=$ ((6+5)): R is a value of 11.



2, minus r=$ ((6-5)): R is a value of 1.



3, Multiply r=$ ((6*5)): R is a value of 30.



4, except r=$ ((6/5)): The value of R is 1. Note that this is divisible.



5, the remainder r=$ ((32%3)): R is a value of 2.



6, Increment 1


r=6
r=$((++r))


The value of row 2,r is incremented by 1 and is assigned to r, so the value of r increments by 1 and becomes 7.



7, Decrement 1


r=6
r=$((--r))


The value of row 2,r is reduced by 1 and then assigned to R, so the value of R is decremented by 1 and becomes 5.



8. Minus 1 after Operation


m=6
r=$((4+m--))


The value of the row 2,m is 6, and 4 is the addition operation, the value is 10, and then assigned to R. Next, the value of M is reduced by 1. Operation Result: R=10,m=5.



If you want to do the arithmetic operation of the unit on the variable itself, you can use (()) to enclose it, which is similar to the syntax of the C language.



As shown below:



1. Specify the operation


((i=168))


The value of the set variable i is 168



2, rear-mounted increment


i=168
((i++))


The value of I is added to 1 and becomes 169.



The 2nd line is changed to a pre-increment ((++i)).



3, the post-type decrement


i=168 ((i--))


The value of I is reduced by 1 and becomes 167.



The 2nd line is changed to a front-facing decrement ((-i)).



4, + =,-=, *=,/= and other assignment operations


i=168 ((i+=60))


The meaning of i+=60 is the same as that of i=i+60, I plus 60后, which assigns the result of the operation to the value of i,i into 228.



5, three-dimensional expression


i=168
((n=i<100?50:80))
echo $n


(()) users actually have the same effect as let, for example: ((i++)) and let "i++" execute the same result. You can use the above (()) usage instead of let.






Section II: Using external program expr for arithmetic operations






An external program, expr, is a value that displays the expression in " standard output ". The syntax is:


Expr ' arithmetic '


For example:


Expr 3 + 2


It will display 5 on the screen.



With this feature, expr can be used as a tool for arithmetic operations. Moreover, since it is an external program, it has little to do with the shell version and can be executed on almost any operating system platform. Therefore, if you focus on cross-platform, portability, you can use expr instead of other arithmetic methods in the development of a script program.



When using expr, pay special attention to whether the "arithmetic" contains special characters for the bash shell, such as *, |, <, > 、!、 &, (,), and so on. If so, use \ to escape, otherwise there will be a strange error message.



In addition, in "arithmetic", the operation conforms to the operand, at least one space space is separated .



The following is an example of expr (note that this is used with a pair of ' symbols, rather than single quotes):



1. r= ' expr parameter 1 \| Parameter 2 '



| means "or". If parameter 1 is present, non-empty, not 0, returns the value of parameter 1, or returns the value of parameter 2. Because | is a special character, so write the escape character \|.



Case:


R= ' Expr 3 \| 0 '


The value of R is 3.


R= ' Expr 0 \| 2 '


The value of 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.



Case:


R= ' expr 3 \& 0 '


3, plus


R= ' Expr 4 + 5 '


The value of R is 9.



4, reduce


r= ' Expr 4-5 '


The value of R is-1.



5, Decrement 1


R=5r= ' expr $r + 1 '





Section III: Using $[] to do arithmetic operations






Use $[] to do arithmetic operations similar to $ (()) .


Syntax: r=$[arithmetic]


The following is a simple arithmetic application:



1, add: r=$[4+5]



2, minus: r=$[4-5]



3, by: R=$[4*5]



4, except: R=$[22/5] (divisible)



5, the remainder: the value of R=$[100%43],r is 14.



6, Increment 1


R=5R=$[R+1]





Section Fourth: Using built-in command declare to do arithmetic operations





Syntax: declare-i variable = formula


For example:


declare -i k
k=8+16

echo $k


Line 1, use declare to set the property of the variable K beforehand as "integer".



Line 2, since K is an integer, 8+16 is no longer a string, but a formula, so bash will perform the operation on it and finally assign the result 24 a K.



It is important to note that:


    • In a calculation, there can be no space between the operator and the operand, and it should be tightly connected.
    • Special symbols do not have to be escaped with \ . For example: multiplication *, multiplication * *.
    • In a calculation, you can include other variables without having to add $ before the variable.


After using declare-i to set variables, subtraction and other operations are very intuitive:


declare -i k

k=8+16
k=16-8
k=5*3
k=22/5
k=2**3
k=100%43

declare -i j
j=k+1  #或 j=$k+1
echo $j


Line 3~8, respectively, are added, minus, multiply, divide, exponentiation, and the remainder of the operation.



Line 10, Set Variable J is also an integer.



Line 11, the calculation can contain variables, but do not have to add ' $ ', K of the variable value into which, plus 1, assigned to the variable J.






Section Fifth: Using built-in commands to do arithmetic operations






Let's use is also very simple, its attention matters with declare.



The following is an example of let:


let k=8+16
let k=16-8
let k=5*3
let k=22/5
let k=2**3
let k=100%43
let j=k+1


Its meaning is self-evident, here will not repeat.



In addition, you can use " whitespace " to make the expression more readable, but you must use " quotation marks " to enclose the expression, such as:


Let "k = k + 5"





Shell Getting Started Tutorial: 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.