Linux / Unix expr command

links: |- back -|- index -|- home -| end

in page: preamble expr notes references

Preamble

The linux / unix expr command evaluates the arguments as a math expression


top

expr command

Syntax

expr expression

Operator Summary

expr1 | expr2 results in the value expr1 if expr1 is true; otherwise it results in the value of expr2.
expr1 & expr2 results in the value of expr1 if both expressions are true; otherwise it results in 0
expr1 <=
expr1 <
expr1 =
expr1 !=
expr1 >=
expr1 >
If both expr1 and expr2 are numeric, expr compares them as numbers; otherwise it compares them as strings. If the comparison is true, the expression results in 1; otherwise it results in 0.
expr1 +
expr1 -
performs addition or subtraction on the two expressions. If either expression is not a number, expr exits with an error. see notes below
expr1 *
expr1 /
expr1 %
performs multiplication, division, or modulus on the two expressions. If either expression is not a number, expr exits with an error. Note that the multiplication symbol (*) is expanded under the KornShell unless you specify it with a leading backslash (\\*), or enclosed in single quotes ('*') or double quotes ("*"). Under command.com you cannot use the backslash to prevent expansion
expr1 : re
match expr1 re
matches the regular expression, 're', against expr1 treated as a string. The regular expression is always anchored, that is, there is an implied leading ^; therefore expr does not consider ^ to be a metacharacter. If the regular expression contains \(...\) and it matches at least part of expr1, then expr results in only that part; if there is no match, expr results in 0. If the regular expression doesn't contain this construct, then the result is the number of characters matched. The function match performs the same operation as the colon operator.
substr expr1 expr2 expr3 results in the substring of expr1 starting at character position expr2 (origin 1) for the length of expr3 characters.
index expr1 expr2 searches for any of the characters in expr2 in expr1 and returns the first character position (origin 1) at which it finds such a character, or 0 if no such characters are found.
length expr1 returns the length of expr1 in characters.
(expr) groups expressions.

Example Using Regular Expression

expr "$VAR" : '.*' - return the number of characters in $VAR.


top

Notes

Just some personal notes on using this 'expr' utility...

1. Strange exit error level of '1' if, with subtraction, the two values are equal, like :-

$ expr 10 - 5
5 - it correctly outputs '5'
$ echo $?
0 - and the error level is zero, but then do
$ expr 10 - 10
0 - again it correctly outputs '0'
$ echo $?
1 - BUT sets an error level of '1'???

Why this error level is '1', just because the two values are EQUAL, and the result is zero?? This has dire consequences if you are running a script with 'set -e', because it will exit the script!!! So in the script it becomes necessary to do 'set +e' before this expr, and then reset to 'set -e' after! - VERY STRANGE...

This took some considerable effort to find out WHY my script was exiting, seemingly without reason...


top

References

1. http://www.computerhope.com/unix/uexpr.htm
2. http://www.tcl.tk/man/tcl8.5/TclCmd/expr.htm
3. http://www.ornl.gov/~wlj/HomePage/unix_help/expr.htm


top

checked by tidy  Valid HTML 4.01 Transitional