expr Command
Intermediate Shell Scripting man(1)Evaluate expressions (arithmetic, string, comparison)
👁 9 views
📅 Updated: Mar 15, 2026
SYNTAX
expr EXPRESSION
What Does expr Do?
expr evaluates expressions and outputs the result. It performs integer arithmetic, string operations, and pattern matching. expr is a legacy tool — most of its functionality is now handled better by bash built-in arithmetic.
expr supports addition, subtraction, multiplication, division, modulo, string length, substring extraction, and regex matching. All operators and operands must be separated by spaces.
While expr is POSIX-standard, bash arithmetic expansion $(( )) is preferred for calculations in modern scripts. expr remains useful in sh-compatible scripts that cannot use bash extensions.
expr supports addition, subtraction, multiplication, division, modulo, string length, substring extraction, and regex matching. All operators and operands must be separated by spaces.
While expr is POSIX-standard, bash arithmetic expansion $(( )) is preferred for calculations in modern scripts. expr remains useful in sh-compatible scripts that cannot use bash extensions.
Options & Flags
| Option | Description | Example |
|---|---|---|
| +,-,*,/,% | Integer arithmetic | expr 10 + 5 |
| length | String length | expr length "hello" |
| substr | Substring extraction | expr substr "hello" 1 3 |
| : | Pattern matching (regex) | expr "file.txt" : ".*\.\(.*\)" |
| =,!= | String comparison | expr "abc" = "abc" |
| <,<=,>,>= | Numeric comparison | expr 10 ">" 5 |
Practical Examples
#1 Integer arithmetic
Adds two numbers.
$ expr 10 + 5
Output:
15
#2 Multiplication
Multiplies — note the backslash to escape * from shell expansion.
$ expr 6 \* 7
Output:
42
#3 String length
Returns the number of characters in the string.
$ expr length "Hello World"
Output:
11
#4 Substring
Extracts 5 characters starting at position 7.
$ expr substr "Hello World" 7 5
Output:
World
#5 Pattern matching
Extracts the year using regex.
$ expr "report_2024.pdf" : ".*_\([0-9]*\)"
Output:
2024
#6 In script assignment
Increments a counter variable.
$ count=$(expr $count + 1)Tips & Best Practices
Use $(( )) instead: Bash arithmetic is simpler and faster: echo $((10 + 5)) instead of expr 10 + 5. Use expr only for POSIX sh scripts.
Escape * for multiplication: expr 6 * 7 fails because * is expanded by the shell. Use expr 6 \* 7 with backslash escape.
Spaces are required: Every operator and operand must be a separate argument: expr 10 + 5 works, expr 10+5 fails.
Frequently Asked Questions
How do I do math in bash?
Use $((expression)): echo $((10 + 5)). Or use expr for POSIX compatibility: expr 10 + 5.
Why does expr multiplication fail?
The * is expanded by the shell before expr sees it. Escape it: expr 6 \* 7 or use quotes: expr 6 '*' 7.
Should I use expr or $(( ))?
Use $(( )) in bash scripts — it is faster and easier. Use expr only in scripts that must be POSIX sh compatible.
Related Commands
More Shell Scripting Commands
Master Linux with Professional eBooks
Curated IT eBooks covering Linux, DevOps, Cloud, and more
Browse Books →