EXPR
SYNTAX
expr expression…
DESCRIPTION
Each token of the expression must be a separate argument.
Operands are either numbers or strings. &qt;expr&qt;&qt; coerces anything
appearing in an operand position to an integer or a string depending on
the operation being applied to it.
Strings are not quoted for &qt;expr&qt;&qt; itself, though you may need to
quote them to protect characters with special meaning to the shell,
e.g., spaces.
Operators may given as infix symbols or prefix keywords. Parentheses
may be used for grouping in the usual manner (you must quote parentheses
to avoid the shell evaluating them, however).
String expressions
——————
&qt;expr&qt;&qt; supports pattern matching and other string operators. These
have lower precedence than both the numeric and relational operators (in
the next sections).
&qt;STRING : REGEX&qt;&qt;
Perform pattern matching. The arguments are coerced to strings
and the second is considered to be a (basic, a la GNU &qt;grep&qt;&qt;)
regular expression, with a &qt;^&qt;&qt; implicitly prepended. The first
argument is then matched against this regular expression.
If the match succeeds and REGEX uses &qt;(&qt;&qt; and &qt;)&qt;&qt;, the &qt;:&qt;&qt;
expression returns the part of STRING that matched the
subexpression; otherwise, it returns the number of characters
matched.
If the match fails, the &qt;:&qt;&qt; operator returns the null string if
&qt;(&qt;&qt; and &qt;)&qt;&qt; are used in REGEX, otherwise 0.
Only the first &qt;( … )&qt;&qt; pair is relevant to the return value;
additional pairs are meaningful only for grouping the regular
expression operators.
In the regular expression, &qt;+&qt;&qt;, &qt;?&qt;&qt;, and &qt;|&qt;&qt; are operators
which respectively match one or more, zero or one, or separate
alternatives. SunOS and other &qt;expr&qt;s treat these as regular
characters. (POSIX allows either behavior.)
&qt;match STRING REGEX&qt;&qt;
An alternative way to do pattern matching. This is the same as
&qt;STRING : REGEX&qt;&qt;.
&qt;substr STRING POSITION LENGTH&qt;&qt;
Returns the substring of STRING beginning at POSITION with length
at most LENGTH. If either POSITION or LENGTH is negative, zero,
or non-numeric, returns the null string.
&qt;index STRING CHARSET&qt;&qt;
Returns the first position in STRING where the first character in
CHARSET was found. If no character in CHARSET is found in STRING,
return 0.
&qt;length STRING&qt;&qt;
Returns the length of STRING.
&qt;quote TOKEN&qt;&qt;
Interpret TOKEN as a string, even if it is a keyword like MATCH or
an operator like &qt;/&qt;&qt;. This makes it possible to test &qt;expr length
quote "$x"&qt;&qt; or &qt;expr quote "$x" : &qt;&qt;.*/(.)&qt; and have it do the
right thing even if the value of $X happens to be (for example)
&qt;/&qt;&qt; or &qt;index&qt;&qt;. This operator is a GNU extension. It is disabled
when the environment variable &qt;POSIXLY_CORRECT&qt;&qt; is set.
To make &qt;expr&qt;&qt; interpret keywords as strings, you must use the
&qt;quote&qt;&qt; operator.
Numeric expressions
——————-
&qt;expr&qt;&qt; supports the usual numeric operators, in order of increasing
precedence. The string operators (previous section) have lower
precedence, the connectives (next section) have higher.
&qt;+ -&qt;&qt;
Addition and subtraction. Both arguments are coerced to numbers;
an error occurs if this cannot be done.
&qt;* / &qt;&qt;&qt;
Multiplication, division, remainder. Both arguments are coerced to
numbers; an error occurs if this cannot be done.
Relations for &qt;expr&qt;&qt;
——————–
&qt;expr&qt;&qt; supports the usual logical connectives and relations. These
are higher precedence than either the string or numeric operators
(previous sections). Here is the list, lowest-precedence operator
first.
&qt;|&qt;&qt; Returns its first argument if that is neither null nor 0,
otherwise its second argument.
&qt;&&qt;&qt; Return its first argument if neither argument is null or 0,
otherwise 0.
&qt;< = >&qt;&qt;
Compare the arguments and return 1 if the relation is true, 0
otherwise. &qt;==&qt;&qt; is a synonym for &qt;=&qt;&qt;. &qt;expr&qt;&qt; first tries to
coerce both arguments to numbers and do a numeric comparison; if
either coercion fails, it does a lexicographic comparison.
Exit status:
————
0 if the expression is neither null nor 0,
1 if the expression is null or 0,
2 for invalid expressions.
Examples
Here are a few examples, including quoting for shell metacharacters.
To add 1 to the shell variable &qt;foo&qt;&qt;, in Bourne-compatible shells:
foo=&qt;expr $foo + 1&qt;
To print the non-directory part of the file name stored in &qt;$fname&qt;&qt;,
which need not contain a &qt;/&qt;&qt;.
expr $fname : &qt;&qt;.*/(^.*)&qt;&qt; &qt;&qt;^|&qt;&qt; $fname
An example showing that &qt;+&qt;&qt; is an operator:
expr aaa : &qt;&qt;a+&qt;&qt;
=> 3
expr abc : &qt;&qt;a(.)c&qt;&qt;
=> b
expr index abcdef cz
=> 3
expr index index a
error–> expr: syntax error
expr index quote index a
=> 0
Hits: 0