Museum

Home

Lab Overview

Retrotechnology Articles

⇒ Online Manual

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

ed(1)

sh(1)

expr(1)                                                             expr(1)

NAME
     expr - evaluate expressions

SYNOPSIS
     expr expression ...

DESCRIPTION
     expr interprets command line arguments as expressions and evaluates
     them in succession. The result of the evaluation is displayed on stan-
     dard output.

     You can use expr to compare strings with one another, for example, or
     to perform calculations with integers in the range of
     -9223372036854775808 to 9223372036854775807.

ARGUMENTS
     expression
          expression may either consist of one operand only, or two
          operands linked by an operator. Any arbitrary string can be
          specified as an operand. Strings that consist of digits only (0
          to 9) are interpreted as integers. Integers preceded by a minus
          sign are interpreted as negative numbers.

          The operands and operators are separated from one another by
          blanks or tabs. If the string you wish to use as an operand
          itself contains blanks or tabs, you must protect them from being
          interpreted as separators by enclosing either the entire string
          or just the blanks or tabs in single or double quotes ('...' or
          "..."). Shell metacharacters must also be quoted ('...' or "...")
          or individually escaped with a backslash [see specialchar(5)].

          If expression consists of only one operand, the result of the
          evaluation will be the operand itself.

          The following section describes how two operands can be linked.
          The operators are arranged in the order of increasing precedence;
          operators with equal precedence are enclosed in braces {...}.

          A result of 0 stands for the value 0, not a null string.

     op1 | op2
          If the evaluation of op1 returns neither a null string nor 0, the
          expression evaluates to op1; otherwise, op2.

     op1 & op2
          If the evaluation of neither op1 nor op2 returns a null string or
          0, the result returned is op1; otherwise, 0.








Page 1                       Reliant UNIX 5.44                Printed 11/98

expr(1)                                                             expr(1)

     op1 rel op2
          rel may be one of the following relational operators:

          <    less than

          <=   less than or equal to

          =    equal to

          >=   greater than or equal to

          >    greater than

          !=   not equal to

          If the condition is fulfilled, the comparison returns a result of
          1. If the condition is not satisfied, the result of the com-
          parison is 0. If both op1 and op2 are integers, the comparison is
          numeric; otherwise, they are interpreted as strings and compared
          alphanumerically.

     op1 {+ -} op2
          If op1 and op2 are both integers, the result returned is the sum
          of, or difference between, the two numbers. If either of the
          arguments is not an integer, expr issues an error message (see
          ERROR MESSAGES)

     op1 {* / %} op2
          When op1 and op2 are integers, the result is equal to the value
          obtained from the specified arithmetic operation:

          *    Multiplication

          /    Division (integers only)

          %    Remaindering

          If either of the arguments is not an integer, expr issues an
          error message (see ERROR MESSAGES).

     op1 : op2
          The strings op1 and op2 are compared with one another, starting
          with the first character in each string and ending with the last
          character in op2. op2 may be specified in the form of a simple
          regular expression [see expressions(5)]. If op1 and op2 match
          each other (i.e. from the first character in both strings to the
          last character in op2), expr usually returns the number of match-
          ing characters. However, if you enter the pattern \(...\) for
          op2, the part of op1 that matches this pattern will be displayed
          (see Example 7 and Example 8).




Page 2                       Reliant UNIX 5.44                Printed 11/98

expr(1)                                                             expr(1)

     match op1 op2
          Same as op1 : op2.

     (...)
          The use of parentheses (...) enables you to influence the
          sequence of evaluation. Parenthesized expressions are evaluated
          first even if they contain operators with lower precedence.

EXIT STATUS
     0    The expression is not incorrect and the evaluation returns nei-
          ther 0 nor a null string.

     1    The evaluation of the expression returns either 0 or a null
          string.

     2    for invalid expressions or division by 0

     >2   Error

ERROR MESSAGES
     expr: non-numeric argument

     You are only allowed to specify integers as operands when using the
     arithmetic operators +, -, *, /, %.

     expr: division by zero

     You have tried to divide by 0.

     expr: syntax error

     You have made a syntax error when calling expr, e.g. you have not de-
     limited operands and operators by blanks or tab.

     expr: RE error

     When using the relational operator : in an expression, you have not
     specified the second operand as a simple regular expression in the
     correct format.

LOCALE
     The LCMESSAGES environment variable governs the language in which
     message texts are displayed.

     In regular expressions in square brackets, the LCCOLLATE environment
     variable governs the scope of character ranges, equivalence classes
     and collating elements, and the

     LCCTYPE environment variable governs the scope of character classes.
     LCCOLLATE also governs the behavior of relational operators in string
     comparisons. Thus if the letters a and ä form part of an equivalence
     class, the expression


Page 3                       Reliant UNIX 5.44                Printed 11/98

expr(1)                                                             expr(1)

     expr $var:[[=a=]]

     returns a value of 1 if the value of the variable is a or ä.

     If LCMESSAGES, LCCOLLATE or LCCTYPE is undefined or is defined as
     the null string, it defaults to the value of LANG. If LANG is likewise
     undefined or null, the system acts as if it were not internationalized.

     If any of the locale variables has an invalid value, the system acts
     as if none of the variables were set.

     The LCALL environment variable governs the entire locale. LCALL
     takes precedence over all the other environment variables which affect
     internationalization.

EXAMPLES
   Example 1

     Simple calculation:

     $ expr 21 + 9 '*' 2 / 6
     24

   Example 2

     A value of 1 is added to variable a in the following example:

     $ echo $a
     3
     $ a=`expr $a + 1`
     $ echo $a
     4

   Example 3

     This example compares two environment variables:

     $ echo $op1 $op2
     text1 text2
     $ expr $op1 = $op2
     0

     If the value of a variable is itself an operator used by expr, the
     comparison will not work. This problem can be solved by combining the
     variable with a "safe" character:

     $ echo $op1 $op2
     = =
     $ expr $op1 = $op2
     expr: syntax error
     $ expr X$op1 = X$op2
     1


Page 4                       Reliant UNIX 5.44                Printed 11/98

expr(1)                                                             expr(1)

   Example 4

     Displaying the number of characters in VAR:

     $ echo $VAR
     Hello
     $ expr $VAR : '.*'
     5

   Example 5

     Comparing two strings:

     $ expr boycott : boy
     3

   Example 6

     Comparison between two strings with the pattern specified as a regular
     expression. The caret ^ is a metacharacter for the shell and must
     therefore be escaped by a backslash \.

     $ expr abc : [\^d-f]
     1

   Example 7

     The path name of a file, e.g. /usr/latino/parnassum/infinitum is
     stored in variable a. To obtain the basic file name, i.e. infinitum,
     you enter:

     $ expr $a : '.*/\(.*\)'
     infinitum

   Example 8

     If variable a contains either the path name or the basename of a file,
     you can extract the file basename as follows:

     $ expr $a : '.*/\(.*\)' \| $a

NOTES
     Many of the operators are also shell control operators, so they have
     to be escaped on the command line.

     Each part of the expression is composed of separate arguments, so
     liberal usage of blank characters is required.







Page 5                       Reliant UNIX 5.44                Printed 11/98

expr(1)                                                             expr(1)

     Examples:
     __________________________________________
    |     Invalid     |          Valid        |
    |_________________|_______________________|
    | expr 1+2        |  expr 1 + 2           |
    | expr "1 + 2"    |  expr 1 + 2           |
    | expr 1 + (2 * 3)|  expr 1 + \( 2 \* 3 \)|
    |_________________|_______________________|

SEE ALSO
     ed(1), sh(1).











































Page 6                       Reliant UNIX 5.44                Printed 11/98

Typewritten Software • bear@typewritten.org • Edmonds, WA 98026