expr - simple expression evaluator
expr expression
The expr(1) utility parses an expression provided as arguments and prints the result on the standard output.
The expr(1) utility recognizes three types of operators: relational operators, arithmetic operators and string operators. The following left-associative binary operators are listed from lowest to highest precedence:
The expr(1) utility exits with 0 if the expression is neither null nor 0, exits with 1 if the expression is null or 0, and exits >1 if there is an error in the expression.
A simple arithmetic calculation; the answer is 8, because division is performed first.
$ expr 4 + 8 / 2
8
Check to see if the value of variable input is hello:
$ expr "$input" = "hello"
List the number of characters in the first directory in your PATH:
$ expr 'echo $PATH' : \[^:]*
Extract the first directory in your PATH; both characters in \( need to be escaped for the shell. An alternate form using single quotes instead of backslashes is also shown:
$ expr "$(echo $PATH)" : \\\(\[^:]*\\\):.*
$ expr "$(echo $PATH)" : '\(\[^:]*\):.*'
When matching strings, expr(1) is best for breaking a string
into two parts; it is not very useful for extracting a particular
word.
Many of the operators are special to the shell and must be quoted.
Operators cannot be used as (string) operands.
sh(1)
test(1)