EXPR(1P)
EXPR(1P) โ the POSIX Programmer's Manual page, but make it a vibe โจ
okay bestie, buckle up, because this is expr from the POSIX Programmer's Manual and she is long. ๐
๐ธ PROLOG ๐ธ
so like, real talk first: this manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (consult the corresponding Linux manual page for details on Linux behavior), or the interface may not be implemented on Linux. iykyk โ always check your local version, fr fr.
๐ NAME ๐
expr โ evaluate arguments as an expression. that's it. that's the whole job. iconic honestly.
๐ฆ SYNOPSIS ๐ฆ
expr operand...
๐ DESCRIPTION ๐
The expr utility shall evaluate an expression and write the result to standard output. like, you hand her some args, she does math or string stuff, she tells you the answer. main character energy, no notes. ๐
โจ OPTIONS โจ
None. zero options, zero flags, she simply does not need them. periodt.
๐ OPERANDS ๐
The single expression evaluated by expr shall be formed from the operand operands, as described in the EXTENDED DESCRIPTION section. (yes, that's coming, keep scrolling bestie ๐ญ)
The application shall ensure that each of the expression operator symbols:
( ) | & = > >= < <= != + - * / % :
and the symbols integer and string in the table are provided as separate arguments to expr. like, one arg each. the shell does not care about your feelings about this. ๐
๐ STDIN ๐
Not used. she doesn't read from you, bestie.
๐ง INPUT FILES ๐ง
None. nothing. empty. clean girl aesthetic.
๐ ENVIRONMENT VARIABLES ๐
The following environment variables shall affect the execution of expr:
LANGโ Provide a default value for the internationalization variables that are unset or null. (See the Base Definitions volume of POSIX.1โ2017, Section 8.2, Internationalization Variables for the precedence of internationalization variables used to determine the values of locale categories.)LC_ALLโ If set to a non-empty string value, override the values of all the other internationalization variables. she said all. ๐LC_COLLATEโ Determine the locale for the behavior of ranges, equivalence classes, and multi-character collating elements within regular expressions and by the string comparison operators.LC_CTYPEโ Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single-byte as opposed to multi-byte characters in arguments) and the behavior of character classes within regular expressions.LC_MESSAGESโ Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error.NLSPATHโ Determine the location of message catalogs for the processing ofLC_MESSAGES.
๐ ASYNCHRONOUS EVENTS ๐
Default. nothing extra, she's chill like that.
โญ STDOUT โญ
The expr utility shall evaluate the expression and write the result, followed by a <newline>, to standard output. yes she adds the newline. every time. ๐ซถ
๐ STDERR ๐
The standard error shall be used only for diagnostic messages. it's the sad-girl channel, only for problems.
๐ธ OUTPUT FILES ๐ธ
None.
๐ EXTENDED DESCRIPTION ๐
okay so THIS is the part you actually came for. The formation of the expression to be evaluated is shown in the following table. The symbols expr, expr1, and expr2 represent expressions formed from integer and string symbols and the expression operator symbols (all separate arguments) by recursive application of the constructs described in the table. The expressions are listed in order of decreasing precedence, with equal-precedence operators grouped between horizontal lines. All of the operators shall be left-associative. (that last bit matters, don't skip it, bestie ๐ซถ)
I'm giving you the table as a list because tables are not the vibe here, but every single row is present, promise. ๐ค
precedence tier 1 โ the babies
integerโ An argument consisting only of an (optional) unary minus followed by digits.stringโ A string argument; see below.
precedence tier 2 โ the group chat
( expr )โ Grouping symbols. Any expression can be placed within parentheses. Parentheses can be nested to a depth of{EXPR_NEST_MAX}.
precedence tier 3 โ the matchmaker
expr1 : expr2โ Matching expression; see below.
precedence tier 4 โ the math girlies
expr1 * expr2โ Multiplication of decimal integer-valued arguments.expr1 / expr2โ Integer division of decimal integer-valued arguments, producing an integer result.expr1 % expr2โ Remainder of integer division of decimal integer-valued arguments.
precedence tier 5 โ plus and minus, obviously
expr1 + expr2โ Addition of decimal integer-valued arguments.expr1 - expr2โ Subtraction of decimal integer-valued arguments.
precedence tier 6 โ the comparison committee
Returns the result of a decimal integer comparison if both arguments are integers; otherwise, returns the result of a string comparison using the locale-specific collation sequence. The result of each comparison is 1 if the specified relationship is true, or 0 if the relationship is false. โจ
expr1 = expr2โ Equal.expr1 > expr2โ Greater than.expr1 >= expr2โ Greater than or equal.expr1 < expr2โ Less than.expr1 <= expr2โ Less than or equal.expr1 != expr2โ Not equal.
precedence tier 7 โ the AND
expr1 & expr2โ Returns the evaluation ofexpr1if neither expression evaluates to null or zero; otherwise, returns zero.
precedence tier 8 โ the OR (lowest precedence, but highest drama)
expr1 | expr2โ Returns the evaluation ofexpr1if it is neither null nor zero; otherwise, returns the evaluation ofexpr2if it is not null; otherwise, zero.
๐ Matching Expression ๐
The ':' matching operator shall compare the string resulting from the evaluation of expr1 with the regular expression pattern resulting from the evaluation of expr2. Regular expression syntax shall be that defined in the Base Definitions volume of POSIX.1โ2017, Section 9.3, Basic Regular Expressions, except that all patterns are anchored to the beginning of the string (that is, only sequences starting at the first character of a string are matched by the regular expression) and, therefore, it is unspecified whether '^' is a special character in that context. ๐
Usually, the matching operator shall return a string representing the number of characters matched ('0' on failure). Alternatively, if the pattern contains at least one regular expression subexpression "[\(...\)]", the string matched by the back-reference expression "\1" shall be returned. If the back-reference expression "\1" does not match, then the null string shall be returned.
ngl the ":" operator is the one that surprises people. she's not a colon, she's a matching colon. respect her. ๐
๐ง Identification as Integer or String ๐ง
An argument or the value of a subexpression that consists only of an optional unary minus followed by digits is a candidate for treatment as an integer if it is used as the left argument to the | operator or as either argument to any of the following operators:
& = > >= < <= != + - * / %
Otherwise, the argument or subexpression value shall be treated as a string. like, the context decides her whole identity. very relatable honestly. ๐ญ
heads up bestie: The use of string argumentslength,substr,index, ormatchproduces unspecified results. unspecified. do not pass go. do not collect $200. ๐
๐ EXIT STATUS ๐
The following exit values shall be returned:
0โ The expression evaluates to neither null nor zero.1โ The expression evaluates to null or zero.2โ Invalid expression.>2โ An error occurred.
๐ CONSEQUENCES OF ERRORS ๐
Default.
The following sections are informative.
and that's part 1, bestie โ part 2 is right there, don't close the tab ๐ฆโจ
๐ APPLICATION USAGE ๐
okay bestie, real talk: the expr utility has a rather difficult syntax ๐ญ and we need to talk about it.
- Many of the operators are also shell control operators or reserved words, so they have to be escaped on the command line. like, the shell is NOT letting you be casual with them.
- Each part of the expression is composed of separate arguments, so liberal usage of
<blank>characters is required. For example:
Here's the invalid vs valid tea โ: -expr 1+2โ invalid, butexpr 1 + 2โ valid -expr "1 + 2"โ invalid, butexpr 1 + 2โ valid -expr 1 + (2 * 3)โ invalid, butexpr 1 + \( 2 \* 3 \)โ valid
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).
๐ธ EXAMPLES ๐ธ
The following command:
a=$(expr "$a" + 1)
adds 1 to the variable a. ๐
The following command, for "$a" equal to either /usr/abc/file or just file:
expr $a : '.*/\(.*\)' \| $a
returns the last segment of a pathname (that is, file). Applications should avoid the character '/' used alone as an argument; expr may interpret it as the division operator.
The following command:
expr "//$a" : '.*/\(.*\)'
is a better representation of the previous example. The addition of the "//" characters eliminates any ambiguity about the division operator and simplifies the whole expression. Also note that pathnames may contain characters contained in the IFS variable and should be quoted to avoid having "$a" expand into multiple arguments.
The following command:
expr "X$VAR" : '.*' - 1
returns the number of characters in VAR.
๐ง RATIONALE ๐ง
In an early proposal, EREs were used in the matching expression syntax. This was changed to BREs to avoid breaking historical applications.
The use of a leading <circumflex> in the BRE is unspecified because many historical implementations have treated it as a special character, despite their system documentation. For example:
expr foo : ^foo expr ^foo : ^foo
return 3 and 0, respectively, on those systems; their documentation would imply the reverse. Thus, the anchoring condition is left unspecified to avoid breaking historical scripts relying on this undocumented feature.
๐ฎ FUTURE DIRECTIONS ๐ฎ
None.
๐ SEE ALSO ๐
- Section 2.5, Parameters and Variables, Section 2.6.4, Arithmetic Expansion
- The Base Definitions volume of POSIX.1โ2017, Section 7.3.2, LC_COLLATE, Chapter 8, Environment Variables, Section 9.3, Basic Regular Expressions, Section 12.2, Utility Syntax Guidelines
๐ COPYRIGHT ๐
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html .
Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .
mwah ๐ this iconic page is from IEEE/The Open Group, dated 2017, and it's EXPR(1P). slay, bestie ๐ โจ
๐ฆ where this page comes from
- alpine 3.22: man-pages-posix 2017a-r0 ยท custom
- alpine 3.23: man-pages-posix 2017a-r0 ยท custom
- alpine 3.24: man-pages-posix 2017a-r0 ยท custom
- arch rolling: man-pages 6.19-1
- arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages-posix 2017a
- opensuse-leap 16.0: man-pages-posix 2017a-160000.4.1 ยท SUSE-IEEE
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms