[LinuxNetworkEnd:Linux&Shell$变量.V09] [Applications.LinuxNetworkEnd][|Linux|shell编程|shell基础|shell数值运算方法|常用运算符|]
一、Shell 常用运算符
运算符 | 说明 | |
13 | - + | 单目负,单目正 |
12 | ! ~ | 逻辑非,按位取反或补码 |
11 | * / % | 乘、处、取模 |
10 | + - | 加 减 |
9 | << >> | 按位左移,按位右移 |
8 | <=>=<> | 小于或等于,大于或等于,小于大于 |
7 | == != | 等于,不等于 |
6 | & | 按位与 |
5 | ^ | 按位异或 |
4 | | | 按位或 |
3 | && | 逻辑与 |
2 | || | 逻辑或 |
1 | = += *= /= %= &= ^= |= <<= >>= | 赋值,运算且赋值 |
### --- 常用运算符实验
~~~ 运算符优先级表明在每个表达式或子表达式中哪一个运算对象首先被求值,
~~~ 数值越大优先级越高,具有较高优先级级别的运算符先于较低级别的运算符进行求值运算。
### --- 例子 1: 加减乘除
~~~ 虽然乘和除的优先级高于加,但是通过小括号可以调整运算优先级
[root@localhost ~]# aa=$(( (11+3)*3/2 ))
[root@localhost ~]# echo $aa
21
### --- 例子 2:取模运算
~~~ 14 不能被 3 整除,余数是 2
[root@localhost ~]# bb=$(( 14%3 ))
[root@localhost ~]# echo $bb
2
### --- 例子 3:逻辑与
~~~ 逻辑与运算只有想与的两边都是 1,与的结果才是 1,否则与的结果是 0
[root@localhost ~]# cc=$(( 1 && 0 ))
[root@localhost ~]# echo $cc
0
===============================END===============================
Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart ——W.S.Landor
来自为知笔记(Wiz)