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 << >> 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 '