exprexpr
arg1 operator arg2
[ operator arg3 ...
]
Evaluate arguments as expressions and print the result. Strings can be compared and searched. Arguments and operators must be separated by spaces. In most cases, an argument is an integer, typed literally or represented by a shell variable. There are three types of operators: arithmetic, relational, and logical. Exit status for expr is 0 (expression is nonzero and nonnull), 1 (expression is 0 or null), or 2 (expression is invalid). expr is typically used in shell scripts to perform simple mathematics, such as addition or subtraction. It is made obsolete in the Korn shell by that program's built-in arithmetic capabilities. Arithmetic OperatorsUse the following operators to produce mathematical expressions whose results are printed:
Addition and subtraction are evaluated last, unless they are grouped inside parentheses. The symbols Relational OperatorsUse relational operators to compare two arguments. Arguments can also be words, in which case comparisons assume
Logical OperatorsUse logical operators to compare two arguments. Depending on the values, the result can be arg1
(or some portion of it), arg2
, or 0. Symbols ExamplesDivision happens first; result is 10:
Addition happens first; result is 7 (truncated from 7.5):
Add 1 to variable
Print 1 (true) if variable
Print 1 (true) if variable
In the following examples, variable
Match all characters and print them:
Print the number of lowercase letters at the beginning of
Match the lowercase letters at the beginning of
Truncate
In a shell script, rename files to their first five letters:
(To avoid overwriting files with similar names, use |
|