EXPR(1)

πŸ“¦ version 1.053, in alpine 3.24

πŸ’… 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 of expr1 if it's neither an empty string nor zero; otherwise, returns the evaluation of expr2. basically "you first, unless you're nothing" πŸ’–
  • expr1 & expr2 β€” returns the evaluation of expr1 if 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 is 1 if the specified relation is true, or 0 if 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 matches expr1 against expr2, 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 πŸ’–

  1. the following example adds one to the variable a. truly the "just one more" of shell scripting ✨
a=`expr $a + 1`
  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 : '.*/\(.*\)'
  1. 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 nor 0.
  • 1 β€” the expression is an empty string or 0.
  • 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 πŸ’œ


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