#Bash 的 let 命令
let ARG...
功能
计算整形算术表达式的值。
类型
Bash 内置命令。
参数
ARG- 要计算的表达式
#运算符
| 运算符 | 说明 |
|---|---|
| id++, id-- | 变量后自增、后自减 |
| ++id, --id | 变量前自增、前自减 |
| -, + | 一元负号、一元正号 |
| !, ~ | 逻辑非、按位取反 |
| ** | 幂运算 |
| *, /, % | 乘法、除法、取余 |
| +, - | 加法、减法 |
| <<, >> | 按位左移、右移 |
| <=, >=, <, > | 比较运算 |
| ==, != | 相等、不相等 |
| & | 按位与 |
| ^ | 按位异或 |
| | | 按位或 |
| && | 逻辑与 |
| || | 逻辑或 |
| expr ? expr : expr | 条件运算符 |
| =, *=, /=, %=, +=, -=, <<=, >>=, &=, ^=, |= | 赋值运算符 |
#示例
$ let x=1+1
$ echo $x
2
$ let x++
$ echo $x
3
#手册
let: let arg [arg ...]
Evaluate arithmetic expressions.
Evaluate each ARG as an arithmetic expression. Evaluation is done in
fixed-width integers with no check for overflow, though division by 0
is trapped and flagged as an error. The following list of operators is
grouped into levels of equal-precedence operators. The levels are listed
in order of decreasing precedence.
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, subtraction
<<, >> left and right bitwise shifts
<=, >=, <, > comparison
==, != equality, inequality
& bitwise AND
^ bitwise XOR
| bitwise OR
&& logical AND
|| logical OR
expr ? expr : expr
conditional operator
=, *=, /=, %=,
+=, -=, <<=, >>=,
&=, ^=, |= assignment
Shell variables are allowed as operands. The name of the variable
is replaced by its value (coerced to a fixed-width integer) within
an expression. The variable need not have its integer attribute
turned on to be used in an expression.
Operators are evaluated in order of precedence. Sub-expressions in
parentheses are evaluated first and may override the precedence
rules above.
Exit Status:
If the last ARG evaluates to 0, let returns 1; let returns 0 otherwise.