Linux expr Command
The expr command is a manual command-line calculator used to evaluate expression variables under UNIX/LINUX. It is generally used for integer values, but can also be used for strings.
Syntax
expr 表达式
Expression description:
- Separate each term with spaces;
- Place a backslash \ before shell-specific characters;
- Enclose strings containing spaces and other special characters in quotes
Example
1. Calculate string length
> expr length “this is a test”
14
2. Extract a substring
> expr substr “this is a test” 3 5
is is
3. Find the position where the first numeric string appears
> 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 Command Reference