Expr

Uses your system voice; voice and rate in settings.

Snapshot

≈ 3 min read

Names in other languages

No names in other languages are held for this entry yet.

Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.

expr is a shell command that evaluates an expression and outputs the result. It evaluates integer or string expressions, including pattern matching regular expressions. Comparison operators (equal, not equal, less than, etc.) apply to both integer and string values. Integer-specific operations include addition, subtraction, multiplication, division and modulus. String-specific operators include: * matching a regular expression ( , or the colon syntax for an arbitrary string "str" and regular expression "reg") * finding a set of characters in a string ( ) * finding a substring ( ) * finding the length of a string ( ) Each symbol (operator, value, etc.) in the expression must be given as a separate parameter. A common challenge with using the command is preventing the shell from acting on input indented to be the expression as something other than expression. Originally developed for Unix v7 and standardized by POSIX, the command is available on IBM i, and for Windows via UnxUtils.

expr is a shell command that evaluates an expression and outputs the result. It evaluates integer or string expressions, including pattern matching regular expressions. Comparison operators (equal, not equal, less than, etc.) apply to both integer and string values. Integer-specific operations include addition, subtraction, multiplication, division and modulus. String-specific operators include: matching a regular expression ( , or the colon syntax for an arbitrary string "str" and regular expression "reg") finding a set of characters in a string ( ) finding a substring ( ) finding the length of a string ( ) Each symbol (operator, value, etc.) in the expression must be given as a separate parameter. A common challenge with using the command is preventing the shell from acting on input indented to be the expression as something other than expression. Originally developed for Unix v7 and standardized by POSIX, the command is available on IBM i, and for Windows via UnxUtils.

Example

The following is a (non-POSIX-compliant) example involving Boolean expressions: expr length "abcdef" " " 8 This example outputs "1". This is because length "abcdef" is 6, which is not less than 5 (so the left side of the | returns zero). But 15 minus 4 is 11 and is greater than 8, so the right side is true, which makes the or true, so 1 is the result. The program exit status is zero for this example. For pure arithmetic, it is often more convenient to use bc. For example: echo "3 * 4 + 14 / 2" | bc since it accepts the expression as a single argument.

Portability

For portable shell programming, use of the "index", "length", "match" and "substr" commands must be avoided; string matching remains possible but it must use the "string : regexp" syntax.

See also

List of POSIX commands

References

External links

expr invocation in GNU coreutils manual

Text derived from https://en.wikipedia.org/w/index.php?oldid=1353507394 under CC BY-SA 4.0; modifications have been made.

Sources cited in this revision

3 named source(s) extracted from the cited revision. A source with no author is shown by its publication; nothing is invented.