Linux expr Command

Linux 命令大全Linux Command Collection

The expr command is a manual command-line counter used to evaluate the value of expression variables under UNIX/LINUX. It is generally used for integer values, and can also be used for strings.

Syntax

expr 表达式

Expression description:

  • Separate each item with spaces;
  • Use backslash\before shell-specific characters;
  • Strings containing spaces and other special characters should be enclosed in quotes.

Examples

1. Calculate string length

> expr length “this is a test”
 14

2. Extract substring

> expr substr “this is a test” 3 5
is is

3. Get the position of the first character/number string occurrence

> expr index "sarasara"  a
 2

4. Integer arithmetic

 > expr 14 % 9
 5
 > expr 10 + 10
 20
 > expr 1000 + 900
 1900
 > expr 30 / 3 / 2
 5
 > expr 30 \* 3 (使用乘号时,必须用反斜线屏蔽其特定含义。因为shell可能会误解显示星号的意义)
 90
 > expr 30 * 3
 expr: Syntax error

Linux 命令大全Linux Command Collection

Other Extensions