Arithmetic operations in the shell

Source: Internet
Author: User
Tags arithmetic bitwise
Work often will write some shell script to complete the task, in the process of writing the shell often involves some arithmetic operations, below I will often use several arithmetic operation commands to do a simple summary. The commonly used arithmetic operation commands are expr, bc,let,$ (())
1. LetLet command is the most I use in the shell of a command, which is simple and easy to support the self-subtraction operation.   I often use it to add an integer value to the for and while loops. The point to note is that we know that the variables in the shell need to be $ notation, but when we use the Let command, we don't need to add $ before the variable.
[Bazar@test/home/bazar] $a =5 [Bazar@test/home/bazar] $let a=a+5 [Bazar@test/home/bazar] $echo $a [bazar@test/home/ Bazar] $let a++ [Bazar@test/home/bazar] $echo $a [Bazar@test/home/bazar] $let a--[Bazar@test/home/bazar] $echo $a [b Azar@test/home/bazar] $let a+=3 [Bazar@test/home/bazar] $echo $a [Bazar@test/home/bazar] $let a=a+0.5-bash:let:a=a+0 .5:syntax error:invalid Arithmetic operator (Error token is ". 5") [bazar@test/home/bazar]$ can see that let does not support floating-point operations, through help lets to Look at the details of let's function
id++, id--variable post-increment, post-decrement ++id,--id variable pre-increment, pre-decrement-, + unary minus, plus !, ~ logical AND bitwise NEGATION * * exponentiation *,/,% multiplication, division, remainder +,-addition, Subtractio N <&lt >> left and right bitwise shifts <=, >=,, > comparison = =,!= equality, inequality & B Itwise and ^ Bitwise XOR | Bitwise OR && Logical AND | | Logical OR expr? expr:expr conditional operator =, *=,/=,%=, + =, =, <<=, >>=, &=, ^=, |= assignment
Through Help information, we can see that let in addition to supporting the subtraction of integers, but also support the logical operation and bit operation, etc. (again up posture:)).
2. ExprThis is another very common command. Expr is also commonly used for integer value operations, such as:
[Bazar@test/home/bazar] $expr + [Bazar@test/home/bazar] $b =20 [Bazar@test/home/bazar] $c = ' expr $b + 5 ' [Bazar@te St/home/bazar] $echo $c [Bazar@test/home/bazar] $d = ' expr $b \* 5 + 10 '
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.