# Expr

> Mediated Wiki article. Canonical URL: https://mediated.wiki/source/Expr
> Markdown URL: https://mediated.wiki/source/Expr.md
> Source: https://en.wikipedia.org/wiki/Expr
> Source revision: 1353507394
> License: Creative Commons Attribution-ShareAlike 4.0 International (https://creativecommons.org/licenses/by-sa/4.0/)

{{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](/source/AT%26T_Bell_Laboratories))
| developer              = Various [open-source](/source/open-source_software) and [commercial](/source/commercial_software) developers
| released               = {{Start date and age|1979}}
| latest release version = 
| latest release date    = 
| operating system       = [Unix](/source/Unix), [Unix-like](/source/Unix-like), [IBM i](/source/IBM_i)
| platform               = [Cross-platform](/source/Cross-platform)
| genre                  = [Command](/source/Command_(computing))
| license                = 
| website                = 
}}
<code>'''expr'''</code> is a [shell](/source/shell_(computing)) [command](/source/command_(computing)) that evaluates an [expression](/source/expression_(mathematics)) and outputs the result. It evaluates [integer](/source/integer) or [string](/source/String_(computer_science)) expressions, including pattern matching [regular expression](/source/regular_expression)s. 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](/source/Version_7_Unix) and standardized by [POSIX](/source/POSIX), the command is available on [IBM i](/source/IBM_i),<ref>{{cite web |title=IBM System i Version 7.2 Programming Qshell |language=en |author=IBM |website=[IBM](/source/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](/source/Windows) via [UnxUtils](/source/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](/source/POSIX)-compliant) example involving [Boolean expression](/source/Boolean_expression)s:
 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](/source/exit_status) is zero for this example.

For pure arithmetic, it is often more convenient to use [bc](/source/Bc_programming_language).  For example:
 echo "3 * 4 + 14 / 2" | bc
since it accepts the expression as a single argument.

===Portability===
For [portable](/source/Software_portability) 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](/source/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}}

---
Adapted from the Wikipedia article [Expr](https://en.wikipedia.org/wiki/Expr) by Wikipedia contributors ([contributor history](https://en.wikipedia.org/wiki/Expr?action=history)). Available under [Creative Commons Attribution-ShareAlike 4.0 International](https://creativecommons.org/licenses/by-sa/4.0/). Changes may have been made.
