Museum

Home

Lab Overview

Retrotechnology Articles

⇒ Online Manual

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

grep(1)

/egrep(1)

/fgrep(1)

sh(1)

test(1)

expr(1)  —  Commands

OSF

NAME

expr − Evaluates arguments as expressions

SYNOPSIS

expr expression

DESCRIPTION

The expr command reads an expression, evaluates it, and writes the result to standard output.  Within the expression argument, you must separate each term with spaces, precede characters special to the shell with a \ (backslash), and quote strings containing spaces or other special characters.  Note that expr returns 0 to indicate a zero value, rather than the null string.  Integers can be preceded by a unary minus sign.  Internally, integers are treated as 32-bit, two’s complement numbers. 

The operators and keywords are described in the following listing.  Characters that need to be escaped are preceded by a \ (backslash).  The list is in order of increasing precedence, with equal precedence operators grouped within {} (braces). 

expression1 \| expression2
Returns expression1 if it is neither null nor 0 (zero); otherwise, returns expression2. 

expression1 \& expression2
Returns expression1 if neither expression1 nor expression2 is null nor 0; otherwise, returns 0. 

expression1 {  =,  \>,  \>=,   \<,  \<=,  !=  } expression2
Returns the result of an integer comparison if both expressions are integers; otherwise, returns the result of a string comparison.

expression1 {+,  - } expression2
Adds or subtracts integer-valued arguments.

expression1 {  \∗,  /,  %  } expression2
Multiplies, divides, or provides the remainder from the division of integer-valued arguments.

expression1 : expression2
Compares expression1 with expression2, which must be a regular expression, with syntax as described for grep, except that all patterns are anchored, so ^ (circumflex) (which anchors a pattern to the beginning of a line) is not a special character in this context.  Normally, the matching operator returns the number of characters matched.  Alternatively, you can use the \(...\) symbols in expression2 to return a portion of expression1.  See OSF/1 User’s Guide for information about collating sequences. 

(expression)
Provides expression grouping.

To avoid unpredictable results when using a range expression to match a class of characters, use a character class expression rather than a standard range expression.  For information about character class expressions, see the discussion of this topic included in the description of the grep command. 

EXAMPLES

     1.To increment a shell variable, enter:

COUNT=‘expr $COUNT + 1‘

This adds 1 to the COUNT shell variable (see sh for details). 

     2.To find the length of a shell variable, enter:

RES=‘expr "$VAR" : ".∗"‘

Note that VAR is in double quotes to avoid problems where VAR is NULL or contains embedded spaces.  The regular expression is also quoted to avoid expansion by the shell. 

     3.To use part of a shell variable, enter:

RES=‘expr "$VAR" : "-∗\(.∗\)"‘

This removes leading - (dashes), if any, from VAR.  If the \( \) characters were omitted, RES would contain the length of VAR. 

     4.Special considerations:

RES=‘expr "x$VAR" : "x-∗\(.∗\)"‘

This succeeds even if VAR has the value - (dash). 

RES=‘expr "x$VAR" = "x="

This succeeds even if VAR has the value = (equal sign). 

EXIT VALUES

The expr command returns the following exit values:

0The expression is neither null nor 0. 

1The expression is null or 0. 

2The expression is invalid. 

RELATED INFORMATION

Commands:  grep(1)/egrep(1)/fgrep(1), sh(1), test(1). 

OSF/1 User’s Guide. 

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