| SH-EXPR(1) | General Commands Manual | SH-EXPR(1) |
expr, ntest, mpexpr - shell module for simple arithmetic.
load expr OR load mpexpr
${expr [ -r radix ] [ arg... ] }
ntest num
Expr and mpexpr are loadable modules for sh (1) that provide support for integer arithmetic. Expr uses 64-bit signed integers; mpexpr uses arbitrary-precision signed integers. They each provide the same interface: a command ntest, which performs a simple boolean test on its integer argument, and the substitution operator expr, which takes an expression in Reverse Polish notation, and yields its result.
Ntest returns true if its argument num is non-zero, and false otherwise.
Expr evaluates each arg in turn; if it is an integer it gets pushed onto the stack; otherwise it should name one of the operators below, whereupon the appropriate number of operands are popped off the stack, evaluated as arguments to the operator, and the result pushed back onto the stack. Arguments are passed to the operator first-pushed first, so, for instance, ${expr 2 1 -} yields 1, not -1. Alternative names are given for some operators; this is to avoid the necessity of quoting operators that contain sh (1) metacharacters. Integers are given in the same form acceptable to Limbo. The relational operators yield either 1 (true) or 0 (false). If the -r option is given, radix specifies an output base for printed numbers. It may be from 2 to 36; mpexpr also allows 64 to specify base64 notation. Numbers are printed in a form suitable for re-interpretation by expr.
When all its arguments have been evaluated, expr yields all the values remaining on its stack, first pushed first. Note that bitwise operators treat their operands as if they were stored in two's complement form. The operators supported by expr are as follows (the number of operands required in is given parentheses).
/appl/cmd/sh/expr.b
Postfix notation can be confusing. Any operators that contain shell metacharacters (e.g. ``*'', ``>'') must be quoted to avoid interpretation by the shell. Base64 notation can contain # characters, which need quoting to avoid interpretation by the shell.