SH-EXPR(1)                                             SH-EXPR(1)

     NAME
          expr, ntest - shell module for simple arithmetic.

     SYNOPSIS
          load expr

          ${expr [ -r radix ] [ arg... ] }
          ntest num

     DESCRIPTION
          Expr is a loadable module for sh(1) that provides support
          for simple integer arithmetic.  It provides one 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.
          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 metacharac-
          ters. All operations use 64-bit signed integers; integers
          are given in the same form acceptable to Limbo. The rela-
          tional operators yield either 1 (true) or 0 (false). If the
          -r option is given, radix specifies an output base for num-
          bers yielded by expr.  Numbers are printed in a form suit-
          able for re-interpretation by expr.

          When all its arguments have been evaluated, expr yields all
          the values remaining on its stack, first pushed first. The
          operators supported by expr are as follows (the number of
          operands required in is given parentheses):

          + (2)          Addition

          - (2)          Subtraction

          x, * (2)       Multiplication

          / (2)          Division. Division by zero raises a divide by
                         zero exception.

          % (2)          Modulus. A zero modulus will cause a divide
                         by zero exception to be raised.

          and (2)        Bitwise-and.

     Page 1                       Plan 9            (printed 11/18/24)

     SH-EXPR(1)                                             SH-EXPR(1)

          or (2)         Bitwise-or.

          xor (2)        Bitwise-xor.

          ~ (1)          Bitwise-complement..

          _ (1)          Unary minus.

          <<, shl (2)    Shift left.

          >>, shr (2)    Shift right.

          =, ==, eq (2)  Equality.

          !=, neq (2)    Inequality.

          >, gt (2)      Greater than.

          <, lt (2)      Less than.

          <=, le (2)     Less than or equal to.

          >=, ge (2)     Greater than or equal to.

          !, not (1)     Logical negation.

          rep (n)        Rep repeats the last operation (which must
                         have been a two-operand operation other than
                         seq) until the values in the stack are
                         exhausted.

          seq (2)        Seq pushes on the stack a sequence of numbers
                         ranging numerically from its first argument
                         up to and including its second argument. If
                         its second argument is less than its first,
                         the sequence will descend.

     SOURCE
          /appl/cmd/sh/expr.b

     SEE ALSO
          sh(1), sh-std(1), sh-tk(1)

     BUGS
          Postfix notation can be confusing.

     Page 2                       Plan 9            (printed 11/18/24)