ARITHMETIC EVALUATION
The shell allows arithmetic expressions to be evaluated, under certain circumstances (see the let and declare builtin commands and Arithmetic Expansion).
Evaluation is done in fixed-width integers with no check for overflow, though division by 0 is trapped and flagged as an error. The operators and their
precedence, associativity, and values are the same as in the C language. The following list of operators is grouped into levels of equal-precedence opera-
tors. The levels are listed in order of decreasing precedence.
id++ id--
variable post-increment and post-decrement
++id --id
variable pre-increment and pre-decrement
- + unary minus and plus
! ~ logical and bitwise negation
** exponentiation
* / % multiplication, division, remainder
+ - addition, subtraction
<< >> left and right bitwise shifts
<= >= < >
comparison== != equality and inequality
& bitwise AND
^ bitwise exclusive OR
| bitwise OR
&& logical AND
|| logical OR
expr?expr:expr
conditional operator= *= /= %= += -= <<= >>=&= ^=|= assignment
expr1 , expr2
comma
可以看到涵盖了通用的编程语言中的数学运算方式,那么在shell中可以通过那些命令来进行计算呢?
declare: 声明变量同时定义其属性。
1234567891011
#!/bin/bashn=1*2*9/3
echo$ndeclare -i n
n=1*2*9/3
echo$nresult:
1*2*9/3
6