expr interprets command-line arguments as expressions and evaluates them in succession. The result of the evaluation is displayed on standard output. You can use expr to compare strings with one another, for example, or to perform calculations with integers.
Syntax
expr 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. Operands and operators are separated from one another by a separator. The value specified for the IFS variable is used as the separator. If you wish to enter a string which contains spaces or tabs as an operand you must therefore enclose the entire string in apostrophes ’...’ or quotes "..." to prevent these characters being interpreted as separators. You must also enclose special shell characters in apostrophes '...' or quotes "..." or quote them by means of the backslash \ (see section “POSIX shell variables and parameter substitution”). 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. |
Linking operators
If op1 is neither the null string nor 0, the expression evaluates to op1; otherwise, op2.
If neither op1 nor op2 is equal to the null string or 0, the result returned is op1; otherwise, 0.
rel may be one of the following relational operators:
If the condition is fulfilled, the comparison returns a result of 1. If the condition is not satisfied, the result of the comparison is 0. If both op1 and op2 are integers, the comparison is numeric; otherwise, they are interpreted as strings and compared alphanumerically.
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 on "expr evaluate arguments as an expression").
When op1 and op2 are integers, the result is equal to the value obtained from the specified arithmetic operation: * If either of the arguments is not an integer, expr issues an error message (see Error on "expr evaluate arguments as an expression").
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 section “Regular POSIX shell expressions”). 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 matching characters. However, if you enter the pattern \(...\) for op2, the part of op1 that matches this pattern will be displayed (see Example 6 and Example 7).
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 | if the expression is neither invalid nor null nor 0 |
1 | if the expression is null or 0 |
2 | for invalid expressions or division by 0 |
Error
You are only allowed to specify integers as operands when using the arithmetic operators
You have tried to divide by 0.
You have made a syntax error when calling expr, e.g. you have not delimited operands and operators by blanks or tab.
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 following environment variables affect the execution of expr: LANG Provide a default value for the internationalization variables that are unset or null. If LANG is unset of null, the corresponding value from the implementation-specific default locale will be used. If any of the internationalization variables contains an invalid setting, the utility will behave as if none of the variables had been defined. LC_ALL If set to a non-empty string value, override the values of all the other internationalization variables. LC_COLLATE Determine the locale for the behavior of ranges, equivalence classes and multicharacter collating elements within regular expressions. LC_COLLATE also governs the behavior of relational operators in string comparisons. Thus if the letters a and ä form part of an equivalence class, the expression LC_CTYPE Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single- as opposed to multi-byte characters in arguments and input files), the classification of characters as upper- to lower-case, and the mapping of characters from one case to the other. LC_MESSAGES Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error. NLSPATH Determine the location of message catalogs for the processing of LC_MESSAGES. |
Example 1
Simple calculation:
|
Example 2
A value of 1 is added to variable a in the following example.
|
Example 3
This example compares two environment variables:
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:
|
Example 4
Displaying the number of characters in VAR:
|
Example 5
Comparing two strings:
|
Example 6
The absolute 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:
|
Example 7
The absolute 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:
|
Example 8
If variable a contains either the absolute path name or the basename of a file, you can extract the file basename as follows:
|