{{Short description|Standard UNIX utility}} {{Lowercase title}} {{Infobox software | name = expr | logo = | screenshot = Expr-example.png | screenshot size = | caption = Example of <code>expr</code> command that does basic summation | author = Dick Haight<br />(AT&T Bell Laboratories) | developer = Various open-source and commercial developers | released = {{Start date and age|1979}} | latest release version = | latest release date = | operating system = Unix, Unix-like, IBM i | platform = Cross-platform | genre = Command | license = | website = }} <code>'''expr'''</code> 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 ({{code|match}}, or the colon syntax {{mono| "''str''" : "''reg''"}} for an arbitrary string "''str''" and regular expression "''reg''") * finding a set of characters in a string ({{code|index}}) * finding a substring ({{code|substr}}) * finding the length of a string ({{code|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,<ref>{{cite web |title=IBM System i Version 7.2 Programming Qshell |language=en |author=IBM |website=IBM |author-link=IBM |url=https://www.ibm.com/support/knowledgecenter/ssw_ibm_i_74/rzahz/rzahzpdf.pdf?view=kc |access-date=2020-09-05 |archive-date=2020-09-18 |archive-url=https://web.archive.org/web/20200918130823/https://www.ibm.com/support/knowledgecenter/ssw_ibm_i_74/rzahz/rzahzpdf.pdf?view=kc |url-status=live }}</ref> and for Windows via UnxUtils.<ref>{{Cite web |url=https://sourceforge.net/projects/unxutils/ |title=Port of the most important GNU utilities to Windows |access-date=2026-02-03 |archive-date=2022-09-07 |archive-url=https://web.archive.org/web/20220907153901/https://sourceforge.net/projects/unxutils/ |url-status=live }}</ref><ref>{{Cite web|url=https://unxutils.sourceforge.net/|title=Native Win32 ports of some GNU utilities|website=unxutils.sourceforge.net|access-date=2026-02-03|archive-date=2006-02-09|archive-url=https://web.archive.org/web/20060209022842/http://unxutils.sourceforge.net/|url-status=live}}</ref>
==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== *List of POSIX commands
==References== {{Reflist}}
==External links== {{Wikibooks|Guide to Unix|Commands}} *{{man|cu|expr|SUS|evaluate arguments as an expression}} *[https://www.gnu.org/software/coreutils/manual/html_node/expr-invocation.html#expr-invocation expr invocation in GNU coreutils manual]
{{Unix commands}} {{Core Utilities commands}}
Category:GNU Project software Category:Unix programming tools Category:Standard Unix programs Category:Unix SUS2008 utilities Category:IBM i Qshell commands
{{unix-stub}}