Linux expr命令教程:数学运算和字符串处理神器(附实例详解和注意事项)

Linux expr 命令介绍

expr 是expression(表达式)的缩写,它是一个用于执行数学运算的命令,除了支持基本的运算符外,expr 还支持逻辑运算和字符串处理功能。expr 命令在shell 脚本编程中广泛应用,特别是在需要进行数学计算或字符串处理的情况下。

Linux expr 命令适用的 Linux 版本

expr 命令在多数Linux发行版上都可使用,包括Ubuntu、Debian、CentOS等。如果在系统上未找到 expr 命令,可以使用下面的命令进行安装:

CentOS 7:

[linux@bashcommandnotfound.cn ~]$ sudo yum install coreutils

CentOS 8:

[linux@bashcommandnotfound.cn ~]$ sudo dnf install coreutils

Ubuntu/Debian:

[linux@bashcommandnotfound.cn ~]$ sudo apt-get install coreutils

Linux expr 命令的基本语法

以下是 expr 命令的基本语法:

expr argument operator argument

Linux expr 命令的常用选项或参数说明

expr 命令没有特定的选项或参数,而是由一系列的数学或者逻辑操作符组成。这些操作符包括:

操作符描述
+加法
-减法
*乘法
/除法
%取模
= , == , != , < , <= , >比较操作符
&逻辑与
|逻辑或

Linux expr 命令实例详解

实例1:使用expr进行加法运算

[linux@bashcommandnotfound.cn ~]$ expr 10 + 20

实例2:使用expr进行除法运算

[linux@bashcommandnotfound.cn ~]$ expr 30 / 3

实例3:使用expr判断字符串长度

[linux@bashcommandnotfound.cn ~]$ expr length "Hello World"

实例4:使用 expr 进行乘法运算

乘法运算时运算符 “*” 需要转义,否则会被 shell 理解为通配符。

[linux@bashcommandnotfound.cn ~]$ expr 10 \* 3

实例5:获取字符串的长度

使用 length 操作符,我们可以获取一个字符串的长度。

[linux@bashcommandnotfound.cn ~]$ expr length "expr command"

实例6:找出目标字符串在给定字符串中最后一次出现的索引

index 操作符可以帮我们完成这个操作。

[linux@bashcommandnotfound.cn ~]$ expr index "expr command" m

实例7:对字符串进行子串操作

substr 操作符可以返回指定索引开始的子字符串,第三个参数指定返回子字符串的长度。

[linux@bashcommandnotfound.cn ~]$ expr substr "expr command" 1 4

实例8:使用 expr 判断是否相等

我们可以使用 expr 来判断两个数是否相等。

[linux@bashcommandnotfound.cn ~]$ expr 2 = 2

实例9:求多个数的和

虽然 expr 是二元运算,但我们可以通过多次调用 expr 来实现多元运算。

[linux@bashcommandnotfound.cn ~]$ expr 2 + 3 + 5

实例10:求余数

我们可以使用 expr 得到两数相除的余数。

[linux@bashcommandnotfound.cn ~]$ expr 10 % 3

实例11: 比较两个数的大小

[linux@bashcommandnotfound.cn ~]$ expr 2 \> 1
[linux@bashcommandnotfound.cn ~]$ expr 2 \< 1

实例12: 使用 expr 命令模拟一个简单的计算器

[linux@bashcommandnotfound.cn ~]$ echo "Please enter two numbers:"
[linux@bashcommandnotfound.cn ~]$ read a b
[linux@bashcommandnotfound.cn ~]$ expr $a + $b
[linux@bashcommandnotfound.cn ~]$ expr $a - $b
[linux@bashcommandnotfound.cn ~]$ expr $a \* $b
[linux@bashcommandnotfound.cn ~]$ expr $a / $b

实例13:比较字符串是否相等

[linux@bashcommandnotfound.cn ~]$ expr "expr" = "expr"

实例14:比较字符串是否不相等

[linux@bashcommandnotfound.cn ~]$ expr "expr" != "command"

实例15:将字符串复制给变量并打印

[linux@bashcommandnotfound.cn ~]$ expr string1="Hello, expr!"
[linux@bashcommandnotfound.cn ~]$ echo $string1

实例16:除法运算并获取余数

[linux@bashcommandnotfound.cn ~]$ expr 10 / 4
[linux@bashcommandnotfound.cn ~]$ expr 10 % 4

实例17:比较两个数大小并返回结果

[linux@bashcommandnotfound.cn ~]$ expr 100 \> 50
[linux@bashcommandnotfound.cn ~]$ expr 100 \< 50

实例18:根据某个字符分割字符串

[linux@bashcommandnotfound.cn ~]$ expr "expr:command" : '\(.*\):.*'
[linux@bashcommandnotfound.cn ~]$ expr "expr:command" : '.*:\(.*\)'

实例19:判断字符串开始的几个字符是否匹配某个字符串

[linux@bashcommandnotfound.cn ~]$ expr "expr command and strings" : 'expr.*'

实例20:使用逻辑运算符

[linux@bashcommandnotfound.cn ~]$ expr 3 \> 2 \| 4 \< 3
[linux@bashcommandnotfound.cn ~]$ expr 3 \< 2 \& 4 \> 3

Linux expr 命令的注意事项

注意当执行乘法运算时,需要用反斜杠”\“转义运算符”*“,正确的运算方式应为”expr 2 * 3“。如果直接写为”expr 2 * 3“,则会出现 bash: expr: command not found 的错误,此时请按照上面的方法进行修复。

Linux expr相关命令

  • 41
    点赞
  • 47
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值