File: sh-utils.info,  Node: expr invocation,  Prev: test invocation,  Up: Conditions

`expr': Evaluate expressions
============================

   `expr' evaluates an expression and writes the result on standard
output.  Each token of the expression must be a separate argument.

   Operands are either numbers or strings.  `expr' 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 `expr' 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).

   Exit status:

     0 if the expression is neither null nor 0,
     1 if the expression is null or 0,
     2 for invalid expressions.

* Menu:

* Relations for expr::          | & < <= = == != >= >
* Numeric expressions::         + - * / %
* String expressions::          <colon> match substr index length
* Examples of expr::            Examples.

File: sh-utils.info,  Node: Relations for expr,  Next: Numeric expressions,  Up: expr invocation

Relations for `expr'
--------------------

   The usual logical connectives and relations, in order of precedence.

`|'
     Yields its first argument if it is neither null nor 0, otherwise
     its second argument.

`&'
     Yields its first argument if neither argument is null or 0,
     otherwise 0.

`< <= = == != >= >'
     Compare the arguments and return 1 if the relation is true, 0
     otherwise.  `==' is a synonym for `='.  `expr' first tries to
     coerce both arguments to numbers and do a numeric comparison; if
     either coercion fails, it does a lexicographic comparison.

File: sh-utils.info,  Node: Numeric expressions,  Next: String expressions,  Prev: Relations for expr,  Up: expr invocation

Numeric expressions
-------------------

   Numeric operators, in order of increasing precedence.  The
connectives (previous section) have higher precedence, the string
operators (following section) have lower.

`+ -'
     Addition and subtraction.  Both arguments are coerced to numbers;
     an error occurs if this cannot be done.

`* / %'
     Multiplication, division, remainder.  Both arguments are coerced to
     numbers; an error occurs if this cannot be done.

File: sh-utils.info,  Node: String expressions,  Next: Examples of expr,  Prev: Numeric expressions,  Up: expr invocation

String expressions
------------------

   String operators.  These have lowest precedence.

`STRING : REGEX'
     Perform pattern matching.  The arguments are coerced to strings
     and the second is considered to be a (basic, a la `grep') regular
     expression, with a `^' implicitly prepended.  The first argument is
     then matched against this regular expression.

     If the match succeeds and REGEX uses `\(' and `\)', the `:'
     expression returns the part of STRING that matched the
     subexpression; otherwise, it returns the number of characters
     matched.

     If the match fails, the `:' operator returns the null string if
     `\(' and `\)' are used in REGEX, otherwise 0.

     Only the first `\( ... \)' pair is relevant to the return value;
     additional pairs are meaningful only for grouping the regular
     expression operators.

     *Note Regular Expression Library: (regex)Top, for details of
     regular expression syntax.

`match STRING REGEX'
     An alternative way to do pattern matching.  This is the same as
     `STRING : REGEX'.

`substr STRING POSITION LENGTH'
     Returns the substring of STRING beginning at POSITION with length
     at most LENGTH.  If either POSITION or LENGTH is negative or
     non-numeric, returns the null string.

`index STRING CHARACTER-CLASS'
     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.

`length STRING'
     Returns the length of STRING.

   The keywords cannot be used as strings.

File: sh-utils.info,  Node: Examples of expr,  Prev: String expressions,  Up: expr invocation

Examples of `expr'
------------------

   Here are a few examples, including quoting for shell metacharacters.

   To add 1 to the shell variable `foo', in Bourne-compatible shells:
     foo=`expr $foo + 1`

   To print the non-directory part of the file name stored in `$fname',
which need not contain a `/'.
     expr $fname : '.*/\(^.*\)' '^|' $fname

     expr abc : 'a\(.\)c'
     => b
     expr index abcdef cz
     => 3
     expr index index a
     error--> expr: syntax error

