linux系統中expr命令

2022-06-08 15:03:10 字數 1323 閱讀 7553

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

Linux下expr命令用法

expr命令詳解 字串長度 expr length this is a test 14 數字求商數 expr 14 9 5從位置處抓取字串 expr substr this is a test 3 5 is is 數字串 only the first character expr index tes...

shell中的expr命令

expr 可以進行的操作如下 邏輯操作 arg1 arg2 邏輯或操作,真則返回arg1,否則返回arg2 以null或者0來判斷引數的真假,有短路功能 arg1 arg2 邏輯與操作,真則返回arg1,否則返回arg2 以null或者0來判斷引數真假,有短路功能 關係操作 arg1 arg2 或者...

bash shell中expr命令下幾種的使用

expr在linux中是乙個功能非常強大的命令。通過學習做乙個小小的總結。1 計算字串的長度。我們可以用awk中的length s 進行計算。我們也可以用echo中的echo 進行計算,當然也可以expr中的expr length string 求出字串的長度。舉例 root localhost s...