EXPR(1)
π EXPR(1) β the perl-adjacent one, bestie
okay so real talk β this is the User Contributed Perl Documentation page for expr, and it's like, the little math-and-string gremlin that lives in your shell. let's get into it π
β¨ NAME β¨
expr β evaluate expression. that's it. that's the whole gig. she evaluates expressions π
π¦ SYNOPSIS π¦
expr expression
πΈ DESCRIPTION πΈ
okay so the expr utility takes expression, evaluates it, and writes the result on standard output. main character energy, honestly β¨
the important vibe check: all operators are separate arguments to the expr utility. like, separate. individual. don't mush them together. and characters that are special to the command interpreter must be escaped β no cap, your shell will absolutely eat them otherwise π
operators are listed below in order of increasing precedence. operators with equal precedence are grouped within { } symbols. iykyk π«Ά
expr1 | expr2β returns the evaluation ofexpr1if it's neither an empty string nor zero; otherwise, returns the evaluation ofexpr2. basically "you first, unless you're nothing" πexpr1 & expr2β returns the evaluation ofexpr1if neither expression evaluates to an empty string or zero; otherwise, returns zero. both gotta show up and both gotta be nonzero, or it's a zero, periodt.expr1 {=, >, >=, <, <=, !=} expr2β returns the results of integer comparison if both arguments are integers; otherwise, returns the results of string comparison using the locale-specific collation sequence. the result of each comparison is1if the specified relation is true, or0if the relation is false. so like, true or false, binary, iconic πexpr1 {+, -} expr2β returns the results of addition or subtraction of integer-valued arguments. math! we love her π§expr1 {*, /, %} expr2β returns the results of multiplication, integer division, or remainder of integer-valued arguments. note the integer part, bestie, this is not the place for your decimals πexpr1 : expr2β the:operator matchesexpr1againstexpr2, which must be a regular expression. the regular expression is anchored to the beginning of the string with an implicit^β so it's matching from the start whether you asked or not, lowkey possessive π
the regular expression language is perlre(1). yes, that's the whole perl regex situation, go off π
if the match succeeds and the pattern contains at least one regular expression subexpression (...), the string corresponding to $1 is returned; otherwise the matching operator returns the number of characters matched. if the match fails and the pattern contains a regular expression subexpression the null string is returned; otherwise 0.
parentheses are used for grouping in the usual manner. classic.
π EXAMPLES π
- the following example adds one to the variable
a. truly the "just one more" of shell scripting β¨
a=`expr $a + 1`
- the following example returns the filename portion of a pathname stored in variable
a. the//characters act to eliminate ambiguity with the division operator β yes she's being careful, we stan a cautious queen π
expr //$a : '.*/\(.*\)'
- the following example returns the number of characters in variable
a. length check, but make it unix π
expr $a : '.*'
π©Ί DIAGNOSTICS π©Ί
the expr utility exits with one of the following values. exit code check, bestie, always:
0β the expression is neither an empty string nor0.1β the expression is an empty string or0.2β the expression is invalid. π (this one's the bad one, fr fr)
π STANDARDS π
the expr utility conforms to IEEE Std1003.2 (``POSIX.2''). certified. licensed. standardised. we love a compliant icon π
π«Ά AUTHOR π«Ά
the original Perl implementation was written by Michael Robinson, smrf@sans.vuw.ac.nz. the current version was written by Michael Mikonos and is based on the C version by John T. Conklin. shoutout to the whole lineage, fr fr π
β COPYRIGHT and LICENSE β
this program may be used under the terms of the Artistic License 2.0. read it, respect it, live it πΈ
reminder: escape your shell-special characters before handing them to expr, or chaos. that's the whole warning. go forth and evaluate, bestie π
β¨
and that's the page!! brought to you by perl v5.42.2, dated 2026-02-15 β mwah, see you in the next manpage πππ¦
π¦ where this page comes from
- alpine 3.24: perl-power-tools-doc 1.053-r1 Β· GPL-2.0-or-later AND Artistic-1.0-Perl AND Artistic-2.0 AND BSD-2-Clause
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms