← 返回命令列表

Linux command

expr 命令

文本

复制后可按需替换文件名、目录或参数。

常用示例

Evaluate arithmetic

expr [5] + [3]

Multiply

expr [5] \* [3]

String length

expr length "[string]"

Substring extraction

expr substr "[string]" [1] [5]

Pattern matching

expr "[string]" : '[regex]'

Compare values

expr [10] \> [5]

Find index

expr index "[string]" "[chars]"

说明

expr evaluates expressions and outputs the result. It handles integer arithmetic, string operations, and comparisons. Results are printed to standard output with exit status indicating boolean results. Operators must be passed as separate arguments, with shell metacharacters escaped. For arithmetic, expr only handles integers. String operations include length, substring extraction, and regex matching. expr is often used in shell scripts for calculations and string manipulation, though modern shells provide built-in alternatives.

参数

+, -, \*, /, %
Arithmetic operators (multiply must be escaped).
=, !=, \<, \>, \<=, \>=
Comparison operators (escape < and >).
length _STRING_
Return string length.
substr _STRING_ _POS_ _LEN_
Extract substring (1-indexed).
index _STRING_ _CHARS_
Find first occurrence of characters.
match _STRING_ _REGEX_
Pattern match (same as STRING : REGEX).
|
Logical OR: ARG1 | ARG2 returns ARG1 if non-null and non-zero, else ARG2.
&
Logical AND: ARG1 & ARG2 returns ARG1 if both are non-null and non-zero, else 0.
--help
Display help information.
--version
Display version information.

FAQ

What is the expr command used for?

expr evaluates expressions and outputs the result. It handles integer arithmetic, string operations, and comparisons. Results are printed to standard output with exit status indicating boolean results. Operators must be passed as separate arguments, with shell metacharacters escaped. For arithmetic, expr only handles integers. String operations include length, substring extraction, and regex matching. expr is often used in shell scripts for calculations and string manipulation, though modern shells provide built-in alternatives.

How do I run a basic expr example?

Run `expr [5] + [3]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

What does +, -, \*, /, % do in expr?

Arithmetic operators (multiply must be escaped).