Путеводитель по Руководству Linux

  User  |  Syst  |  Libr  |  Device  |  Files  |  Other  |  Admin  |  Head  |



   expr.1p    ( 1 )

оценивать аргументы как выражение (evaluate arguments as an expression)

Использование в приложениях (Application usage)

The expr utility has a rather difficult syntax:

* Many of the operators are also shell control operators or reserved words, so they have to be escaped on the command line.

* Each part of the expression is composed of separate arguments, so liberal usage of <blank> characters is required. For example:

┌─────────────────┬───────────────────────┐ │ Invalid Valid │ ├─────────────────┼───────────────────────┤ │expr 1+2 │ expr 1 + 2 │ │expr "1 + 2" │ expr 1 + 2 │ │expr 1 + (2 * 3) │ expr 1 + \( 2 \* 3 \) │ └─────────────────┴───────────────────────┘ In many cases, the arithmetic and string features provided as part of the shell command language are easier to use than their equivalents in expr. Newly written scripts should avoid expr in favor of the new features within the shell; see Section 2.5, Parameters and Variables and Section 2.6.4, Arithmetic Expansion.

After argument processing by the shell, expr is not required to be able to tell the difference between an operator and an operand except by the value. If "$a" is '=', the command:

expr "$a" = '='

looks like:

expr = = =

as the arguments are passed to expr (and they all may be taken as the '=' operator). The following works reliably:

expr "X$a" = X=

Also note that this volume of POSIX.1‐2017 permits implementations to extend utilities. The expr utility permits the integer arguments to be preceded with a unary minus. This means that an integer argument could look like an option. Therefore, the conforming application must employ the "--" construct of Guideline 10 of the Base Definitions volume of POSIX.1‐2017, Section 12.2, Utility Syntax Guidelines to protect its operands if there is any chance the first operand might be a negative integer (or any string with a leading minus).

For testing string equality the test utility is preferred over expr, as it is usually implemented as a shell built-in. However, the functionality is not quite the same because the expr = and != operators check whether strings collate equally, whereas test checks whether they are identical. Therefore, they can produce different results in locales where the collation sequence does not have a total ordering of all characters (see the Base Definitions volume of POSIX.1‐2017, Section 7.3.2, LC_COLLATE).