EXPR(1) COMMAND REFERENCE EXPR(1) NAME expr - evaluate arguments as an expression SYNOPSIS expr arg ... DESCRIPTION The arguments are taken as an expression. After evaluation, the result is written on the standard output. Each token of the expression is a separate argument. Expressions are expected to be integers and strings. Floating point numbers are not supported. The operators and keywords are listed below. The list is in order of increasing precedence, with equal precedence operators grouped. expression | expression yields the first expression if it is neither null nor `0', otherwise yields the second expression. expression & expression yields the first expression if neither expression is null or `0', otherwise yields `0'. expression relop expression where relop is one of < <= = != >= > , yields `1' if the indicated comparison is true, `0' if false. The comparison is numeric if both expression are integers, otherwise lexicographic. expression + expression expression - expression addition or subtraction of the arguments. expression * expression expression / expression expression % expression multiplication, division, or remainder of the arguments. expression : expression The matching operator compares the string first argument with the regular expression second argument; regular expression syntax is the same as that of ed(1). The \(...\) pattern symbols can be used to select a portion of the first argument. Otherwise, the matching operator yields the number of characters matched (`0' on failure). match expression expression This is a synonym for the : operator. substr string start length Printed 4/6/89 1
EXPR(1) COMMAND REFERENCE EXPR(1) The substring operator yields the portion of string beginning at position start that is a maximum of length characters long. start must be an expression which yields a positive integer, and length must be an expression which yields a non-negative integer. length expression The length operator yields the length of the string expression. index string character-list The index operator yields the position of the first occurrence of any character in character-list found in string. ( expression ) parentheses for grouping. EXAMPLES To add 1 to the Shell variable a (where a is already set) : a=`expr $a + 1` To find the filename part (least significant part) of the pathname stored in variable a, which may or may not contain `/' (this is partially equivalent to basename(1) ): expr $a : '.*/\(.*\)' '|' $a Note the quoted Shell metacharacters. This example prints the portion of the home directory name following the first '/' character after the first character in the name. (Note the escaped /, which would normally mean division.): expr substr \ \( substr $HOME 2 1024 \) \ \( index \( substr $HOME 2 1024 \) '\/' \) + 1 \ 1024 If $HOME is /usr/guest , the above expression would print 'guest'. RETURN VALUE [0] The expression is neither null nor '0'. [1] The expression is either null or '0'. [2] The expression contains errors. Printed 4/6/89 2
EXPR(1) COMMAND REFERENCE EXPR(1) CAVEATS The strings substr, index, length, and match may appear only as operators, and not as strings. Therefore, an expression like 'substr : [ubs]*' results in a syntax error. SEE ALSO basename(1), sh(1sh), and test(1sh). Printed 4/6/89 3
%%index%% na:240,94; sy:334,178; de:512,2790;3614,952; ex:4566,935; rv:5501,297; ca:6110,308; se:6418,208; %%index%%000000000130