Open System Services Shell and Utilities Reference Manual (G06.29+, H06.08+, J06.03+)

expr(1) OSS Shell and Utilities Reference Manual
EXAMPLES
1. To increment a shell variable, enter:
COUNT=‘expr $COUNT + 1‘
This command adds 1 to the COUNT shell variable (see the sh command reference page
for details).
2. To find the length of a shell variable, enter:
RES=‘expr "$VAR" : ".*"‘
Note that the VAR variable is placed within double quotation marks 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 the VAR variable. If the \( \) characters are
omitted, the RES variable would contain the length of VAR.
4. Special considerations:
RES=‘expr "x$VAR" : "x-*\(.*\)"‘
This command succeeds even if the VAR variable has the value - (dash).
RES=‘expr "x$VAR" = "x="
This succeeds even if the VAR variable has the value = (equal sign).
EXIT VALUES
The expr command returns the following exit values:
0 The expression is neither null nor 0 (zero).
1 The expression is null or 0 (zero).
2 The expression is invalid.
>2 An error occurred.
RELATED INFORMATION
Commands: grep(1), sh(1), test(1).
3126 Hewlett-Packard Company 527188-021