expr
Page Module:Infobox/styles.css has no content.
| expr | |
|---|---|
| Lua error in package.lua at line 80: module 'Module:InfoboxImage/data' not found. Example of expr command that does basic summation | |
| [[Programmer|Original authorTemplate:Pluralize from text]] | Dick Haight (AT&T Bell Laboratories) |
| [[Programmer|DeveloperTemplate:Pluralize from text]] | Various open-source and commercial developers |
| Initial release | 1979 |
| Engine | Page Template:Plainlist/styles.css has no content.Template:EditAtWikidata |
| Operating system | Unix, Unix-like, IBM i |
| Platform | Cross-platform |
| Type | Command |
| Website | Script error: No such module "WikidataIB". |
| Repository | Page Template:Plainlist/styles.css has no content.
|
Script error: No such module "Check for conflicting parameters".
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 (
match, or the colon syntax Page Template:Mono/styles.css has no content. "str" : "reg" for an arbitrary string "str" and regular expression "reg") - finding a set of characters in a string (
index) - finding a substring (
substr) - finding the length of a string (
length)
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,[1] and for Windows via UnxUtils.[2][3]
Example
The following is a (non-POSIX-compliant) example involving Boolean expressions:
expr length "abcdef" "<" 5 "|" 15 - 4 ">" 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
References
Page Template:Reflist/styles.css has no content.
- ^ Page Module:Citation/CS1/styles.css has no content.IBM. "IBM System i Version 7.2 Programming Qshell" (PDF). IBM. Archived (PDF) from the original on 2020-09-18. Retrieved 2020-09-05.
- ^ Page Module:Citation/CS1/styles.css has no content."Port of the most important GNU utilities to Windows". Archived from the original on 2022-09-07. Retrieved 2026-02-03.
- ^ Page Module:Citation/CS1/styles.css has no content."Native Win32 ports of some GNU utilities". unxutils.sourceforge.net. Archived from the original on 2006-02-09. Retrieved 2026-02-03.
External links
Page Module:Side box/styles.css has no content.Page Template:Sister project/styles.css has no content.
Lua error in package.lua at line 80: module 'Module:Navbox/configuration' not found. Template:Core Utilities commands