manpagez: man pages & more
info coreutils
Home | html | info | man

File: coreutils.info,  Node: expr invocation,  Prev: test invocation,  Up: Conditions

16.4 ‘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 integers or strings.  Integers consist of one or
more decimal digits, with an optional leading ‘-’.  ‘expr’ converts
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.  However, regardless of whether it is quoted, a string
operand should not be a parenthesis or any of ‘expr’’s operators like
‘+’, so you cannot safely pass an arbitrary string ‘$str’ to expr merely
by quoting it to the shell.  One way to work around this is to use the
GNU extension ‘+’, (e.g., ‘+ "$str" = foo’); a more portable way is to
use ‘" $str"’ and to adjust the rest of the expression to take the
leading space into account (e.g., ‘" $str" = " foo"’).

   You should not pass a negative integer or a string with leading ‘-’
as ‘expr’’s first argument, as it might be misinterpreted as an option;
this can be avoided by parenthesization.  Also, portable scripts should
not use a string operand that happens to take the form of an integer;
this can be worked around by inserting leading spaces as mentioned
above.

   Operators may be given as infix symbols or prefix keywords.
Parentheses may be used for grouping in the usual manner.  You must
quote parentheses and many operators to avoid the shell evaluating them,
however.

   Because ‘expr’ uses multiple-precision arithmetic, it works with
integers wider than those of machine registers.

   The only options are ‘--help’ and ‘--version’.  *Note Common
options::.  Options must precede operands.

   Exit status:

     0 if the expression is neither null nor 0,
     1 if the expression is null or 0,
     2 if the expression is invalid,
     3 if an internal error occurred (e.g., arithmetic overflow).

* Menu:

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

© manpagez.com 2000-2024
Individual documents may contain additional copyright information.