expr command (evaluates an expression)
- Organization of this page
Format
expr expression
Description
This command evaluates an expression and sends the results to the standard output. All elements of the expression are specified as separate arguments.
An expression is specified as a combination of numeric values, character strings, variables, expressions, and their operators. The evaluation of the expression is retained as a character string or integer.
Arguments
- expression
-
Specifies the expression to be evaluated. The operators are shown below in increasing order of priority. Operators shown enclosed in curly brackets ({}) and separated by the comma have the same priority. When an argument is invalid, the expr command outputs an error message and returns 2 as the return code. Expressions are denoted by expr1 and expr2.
- expr1| expr2
-
If expr1 is not an empty character string or zero, the evaluation of expr1 is returned. If expr1 is an empty character string or zero, the evaluation of expr2 is returned. If expr2 is also a null character string, the null character string is returned.
- expr1 & expr2
-
If neither expression evaluates to an empty character string or zero, the evaluation of expr1 is returned; otherwise, 0 is returned.
- expr1 {=, >, >=, <, <=, !=} expr2
-
If both expressions evaluate to an integer, the result of comparing the integers is returned; otherwise, the result of comparing the character strings in the collating sequence defined locally is returned. The result is 1 if the specified relationship is true, and 0 if it is false.
-
=: The values on the left and right are equal.
-
>: The value on the left is greater than the value on the right.
-
>=: The value on the left is greater than or equal to the value on the right.
-
<: The value on the left is less than the value on the right.
-
<=: The value on the left is less than or equal to the value on the right.
-
!=: The values on the left and right are not equal.
-
- expr1 {+, -} expr2
-
If both expressions evaluate to integer values, the result of the addition or subtraction is returned.
If the value is not an integer, an error message (expr: non-numeric argument) is output.
-
+: Add
-
-: Subtract
-
- expr1 {*, /, %} expr2
-
If both expressions evaluate to integer values, the result of the multiplication, division, or modulo operation is returned. If the values are not integers, an error message (expr: non-numeric argument) is output. If the divisor is zero, an error message (expr: division by zero) is output.
-
*: Multiply
-
/: Divide
-
%: Modulo
-
- expr1 : expr2
-
Evaluates whether expr2 matches expr1.
The expression expr2 is specified as a regular expression. The regular expression is treated as if ^ were added at the beginning of the string.
-
If a tagged regular expression is specified in expr2 (if expr2 matches expr1), the first character string that matched the tagged regular expression is returned.
-
If a tagged regular expression is not specified in expr2 (if expr2 matches expr1), the number of matched characters is returned.
-
If expr2 does not match expr1, and a regular expression is used in expr2, the null character is returned. If a regular expression is not used in expr2, 0 is returned.
-
If the specification of expr2 matches the null character, 0 is returned. To determine if expr1 is the null character, you must evaluate it by assigning the same character to both expr1 and expr2. That is, instead of expr '' : '$', you must use expr X'' : 'X$', or a similar variation.
-
- length character-string
-
Returns the length of the specified character string. For details about the ADSH_CMDEXPR_LENGTH environment variable, see 2.5 Specifying environment variables.
-
If the ADSH_CMDEXPR_LENGTH=b environment variable is set, the command treats length as an operator and returns the length (in bytes) of the character string that follows.
-
If the ADSH_CMDEXPR_LENGTH=c environment variable is set, the command treats length as an operator and returns the length (in characters) of the character string that follows.
-
If the ADSH_CMDEXPR_LENGTH environment variable is not set or a value other than b or c is set in the environment variable, the command does not treat length as an operator.
You can specify an expression in the length operator. If you specify an expression, enclose the entire expression is parentheses (()).
-
Return codes
Return code |
Meaning |
---|---|
0 |
Normal termination. The expression is not an empty character string or 0. |
1 |
Normal termination. The expression is an empty character string or 0. |
2 |
Error termination. The expression is not valid. |
3 or greater |
Error termination
|
Notes
-
Integer values are stored in the range of -2147483648 to 2147483647. If you specify a value outside this range, any overflow into the 32-bit binary digit position will be ignored.
-
Characters specified in the operators and parentheses, because they include characters that are interpreted by the shell, must be properly escaped. Because the entire expression is interpreted as a character string when it is enclosed in double quotation marks ("), each individual operator must be enclosed in double quotation marks (").
-
This command does not accept options. If you specify an option as an argument, the option is interpreted as an expression.
Usage examples
-
Perform a calculation using variable a and variable b.
$ a=2 $ b=3 $ x=`expr \( $a + $b \) \* 10` $ echo $? 0 $ echo $x 50 $
-
Evaluate variable a | variable b.
$ a="" $ b="abcdef" $ expr "$a" \| "$b" abcdef $
-
Cut the file name from the path name, without the extension.
$ a='d:\jp1as\test.txt' $ expr $a : '.*\\\(.*\)\.' test $
-
Determine whether a variable includes numbers. The return will be 0 if there are no numbers.
$ a='abcde12345kl' $ b='abcdefg' $ expr $a : '.*[0-9].*' 12 $ expr $b : '.*[0-9].*' 0 $
-
Return the length of character string in bytes.
$ export ADSH_CMDEXPR_LENGTH=b $ echo $LANG ja_JP.UTF-8 $ expr length "" 18
-
Return the length of character string in characters.
$ export ADSH_CMDEXPR_LENGTH=c $ echo $LANG ja_JP.UTF-8 $ expr length "" 6
-
Return a value that is obtained by adding 2 to the length of character string teststring (in bytes).
$ export ADSH_CMDEXPR_LENGTH=b $ echo $LANG ja_JP.UTF-8 $ expr length teststring + 2 12