简介
Linux 中的 expr 命令用于计算表达式的值,包括算术运算、字符串操作和逻辑比较。它常用于 shell 脚本。
基本算术运算
expr 支持基本算术运算,例如加、减、乘、除和模数
- 加(+)
expr 5 + 3
# Output: 8
- 减(-)
expr 10 - 4
# Output: 6
- 乘(*)
* 运算符必须进行转义(\*)以防止 shell 解释
expr 6 \* 3
# Output: 18
- 除(/)
expr 12 / 4
# Output: 3
- 取模(%)
expr 10 % 3
# Output: 1
字符串操作
计算字符串长度
expr length "Hello"
# Output: 5
提取字符串
expr substr "HelloWorld" 1 5
# Output: Hello
查找字符位置
expr index "LinuxShell" 'S'
# Output: 6
比较操作
检查是否相等(=)
expr "apple" = "apple"
# Output: 1 (true)
检查是否不相等(!=)
expr "apple" != "banana"
# Output: 1 (true)
比大小
> 和 < 必须用 \ 进行转义,以防止被 shell 解释
expr 10 \> 5
# Output: 1 (true)
expr 2 \< 8
# Output: 1 (true)
逻辑操作
AND(&)
expr 4 \> 2 \& 3 \> 1
# Output: 1 (true)
OR(|)
expr 4 \< 2 \ 3 \> 1
# Output: 1 (true)
在shell脚本中使用expr
#!/bin/bash
a=10
b=5
sum=$(expr $a + $b)
echo "Sum: $sum"
# Output: Sum: 15
expr 的替代方案
- 使用 $(()) 进行算术运算
echo $((5 + 3))
- 使用 bc 进行浮点计算
echo "scale=2; 10 / 3" | bc