1、linux系統中expr命令實現命令行中的四則運算
簡單示例:
[root@linuxprobe test]# expr 5 + 3 ## 在命令行中實現加法運算
8
2、中間必須有空格
[root@linuxprobe test]# expr 5+3 ##中間必須有空格
5+3
[root@linuxprobe test]# expr 5 +3 ##同上
expr: syntax error: unexpected argument ‘+3’
[root@linuxprobe test]# expr 5+ 3 ## 同上
expr: syntax error: unexpected argument ‘3’
3、必須是整數運算
[root@linuxprobe test]# expr 5 + 3
8 [root@linuxprobe test]# expr 5.5 + 3 ## 必須是整數運算 expr: non-integer argument
4、減法運算
[root@linuxprobe test]# expr 10 - 4
6 [root@linuxprobe test]# expr 10 + 5 - 2
13
5、乘法運算
[root@linuxprobe test]# ls [root@linuxprobe test]# expr 3 * 5 ## 當前目錄為空時,*前可以不加轉義
15 [root@linuxprobe test]# touch a.txt [root@linuxprobe test]# ls a.txt [root@linuxprobe test]# expr 3 * 5 expr: syntax error: unexpected argument ‘a.txt’ [root@linuxprobe test]# expr 3 \* 5 ## 加轉義
15
6、除法、取余
[root@linuxprobe test]# expr 10 / 2
5 [root@linuxprobe test]# expr 10 / 3 ## 除法只保留整數
3 [root@linuxprobe test]# expr 10 % 3 ##取余數
1