Museum

Home

Lab Overview

Retrotechnology Articles

Online Manuals

⇒ expr(C) — OpenDesktop 3.0.0

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

awk(C)

basename(C)

bc(C)

locale(M)

coltbl(M)

sh(C)


 expr(C)                         19 June 1992                         expr(C)


 Name

    expr - evaluate arguments as an expression

 Syntax

    expr arguments

 Description

    The arguments are taken as an expression.  After evaluation, the result
    is written on the standard output.  Terms of the expression must be
    separated by blanks.  Characters special to the shell must be escaped.
    Note that 0 is returned to indicate a zero value, rather than the null
    string.  Strings containing blanks or other special characters should be
    quoted.  Integer-valued arguments may be preceded by a unary minus sign.
    Internally, integers are treated as 32-bit, 2's complement numbers.

    The operators and keywords are listed below.  Individual parameters
    within expressions may need to be quoted or escaped, since many of the
    characters that have special meaning in the shell also have special mean-
    ing in expr.  The list is in order of increasing precedence, with equal
    precedence operators grouped within braces ( { and } ).  Parentheses ( )
    can be used for grouping; see the Examples section below for the syntax.

    expr is useful for performing variable arithmetic and other variable
    manipulation within shell scripts.  See the Examples section below for
    some ideas.

    arg | arg         Returns the first arg if it is neither null nor 0, oth-
                      erwise returns the second arg.

    arg & arg         Returns the first arg if neither arg is null nor 0,
                      otherwise returns 0.

    arg { =, ==, >, >=, <, <=, != } arg
                      Returns the result of an integer comparison if both
                      arguments are integers, otherwise returns the result of
                      a lexical comparison, as defined by the locale.  The
                      result will be 1 if the expression is true and 0 if the
                      expression is false.  The double equals sign (==) does
                      the same thing as the single equals sign (=); it is
                      simply an alternative syntax.

    arg { *, /, % } arg
                      Multiplication, division, or remainder of the integer-
                      valued arguments.

    arg { +, - } arg  Addition or subtraction of integer-valued arguments.

    arg : arg         The matching operator (:) compares the first argument
                      with the second argument, which must be a regular
                      expression; regular expression syntax is the same as
                      that of ed(C), except that all patterns are
                      ``anchored'' (that is, begin with a caret (^)) and
                      therefore the caret is not a special character in that
                      context.  (Note that in the shell, the caret has the
                      same meaning as the pipe symbol (|).)  Normally the
                      matching operator returns the number of characters
                      matched (0 on failure).  Alternatively, the \(...\)
                      pattern symbols can be used to return a portion of the
                      first argument.

    match string rexp The match operator is identical in function to the
                      colon operator (:) described above, but with a dif-
                      ferent syntax.

    substr string x y The substring operator takes three arguments: a string,
                      an integer index into the string, x; and the number of
                      characters to return from the string, y.  substr goes
                      to the xth character in string and returns the next y
                      characters.  If y is greater than the number of remain-
                      ing characters in the string, expr will return the
                      remainder of the string.  x must be an integer greater
                      than 0; y must be a positive integer (0 is acceptable,
                      if you want 0 as the result).  See the following sec-
                      tion for an example.

    length string     The length operator returns the length (the number of
                      characters) of string.

    index string r [stuv]
                      The index operator returns an integer indicating the
                      place of r in string.  If r is not in string, 0 is
                      returned.  You can specify as many characters as you
                      like in the second argument; expr will then take the
                      first character which appears in string and return its
                      place in the string as an integer.  See the following
                      section for an example.

 Examples

    This is an example of how expr can be used in a shell script to do vari-
    able arithmetic:

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

    Parentheses can be placed around the part of an expression you want
    evaluated first.  Be careful with the syntax; the backslashes and whi-
    tespace are essential:

       expr \( 1 + 2 \) \* 10
       30

    The matching operator in expr (: or match) can be used to return a por-
    tion of a pathname:

       a=/usr/lulu/valentines/woowoo
       expr $a : '.*/\(.*\)'
       woowoo

    basename(C) does the same thing, however, and uses a simpler syntax:

       a=/usr/lulu/valentines/woowoo
       basename $a
       woowoo

    You can use the length operator to check the length of a string variable,
    and assign this value to another variable, if you like:

       a=/usr/lulu/valentines/woowoo
       b=`expr length $a`
       echo $b
       27

    The substring (substr) operator pulls out a specific part of a string:

       expr substr mongoose 4 7
       goose

    Here, the expr substring operator returns a substring of ``mongoose''
    specified by 4 (start from the fourth character) and 7 (give me the next
    seven characters).  Note that there are not seven more characters in
    ``mongoose'' from the ``g'', so expr only returns what is left.

    The index operator tells you the place of a character in a string:

       expr index wombat zoqb
       2

    In this example, the index operator takes the ``o'', the first character
    that is actually in the string ``wombat'', and returns its place in the
    string.  expr index wombat o would have the same result.

 See also

    awk(C), basename(C), bc(C), locale(M), coltbl(M), sh(C)

 Diagnostics

    As a side effect of expression evaluation, expr returns the following
    exit values:

       0       If the expression is neither null nor zero
       1       If the expression is null or zero
       2       For invalid expressions

    Other diagnostics include:

    syntax error   For operator/operand errors, including unset variables

    nonnumeric argument
                   If arithmetic is attempted on a nonnumeric string

 Notes

    After argument processing by the shell, expr cannot tell the difference
    between an operator and an operand except by the value.  If $a is an
    equals sign (=), the command:

       expr  $a  =  "="

    looks like:

       expr  =  =  =

    The arguments are passed to expr and will all be taken as the = operator.
    The following permits comparing equals signs:

       expr  X$a  =  X=


 Standards conformance

    expr is conformant with:

    AT&T SVID Issue 2;
    and X/Open Portability Guide, Issue 3, 1989.


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