How to Use the expr Command to Perform Mathematical Calculations in Linux

The Terminal as a Calculator

If you are a systems administrator writing a complex Linux bash script that automatically deletes old backup files, your script must be able to perform basic mathematics. It needs to calculate exactly how many days have passed since a file was created, subtract the threshold limit, and determine if the file should be vaporized.

Unlike massive programming languages such as Python or C++, the standard Linux terminal does not natively understand how to perform arithmetic on its own. If you simply type 5 + 5 into the terminal and press Enter, Linux will throw an error, assuming you are trying to execute a program named “5” that does not exist.

To mathematically force the terminal to interpret numbers as actual mathematical values and execute addition, subtraction, multiplication, and division directly from the command line, you must use the expr (Expression) command.

Step 1: Basic Addition and Subtraction

The expr command requires absolute, perfect spacing. Every single number and mathematical symbol (operator) must be separated by a single physical space. If you jam the numbers together, the command will fail.

To add two numbers together, type:

expr 10 + 25

The terminal will instantly output 35. If you typed expr 10+25 (without spaces), the terminal would simply echo the exact text “10+25” back to you, failing to recognize it as a mathematical equation.

To perform subtraction, use the standard hyphen:

expr 50 - 15

The terminal will instantly output 35.

Step 2: The Multiplication Trap

Multiplication is where the expr command becomes incredibly dangerous for beginners. In mathematics, the standard symbol for multiplication on a computer is the asterisk (*). However, in the Linux terminal, the asterisk is a highly protected “wildcard” character used to select multiple files.

If you type expr 5 * 5, the terminal will instantly crash and throw a massive syntax error. Linux thinks you are trying to multiply the number 5 by every single file located in your current directory.

To mathematically multiply two numbers, you must use the backslash (\) to “escape” the asterisk. This tells Linux: “Do not treat this as a wildcard; treat this as a literal mathematical symbol.”

expr 5 \* 5

The terminal will now safely execute the equation and output 25.

Step 3: Division and Remainders

To divide two numbers, you use the standard forward slash (/).

expr 100 / 4

The terminal will output 25. However, it is critical to understand that the standard expr command only understands integers (whole numbers). It completely ignores decimals. If you type expr 10 / 3, the terminal will output 3 (not 3.333).

If you need to know the mathematical remainder of that division, you must use the modulo operator (%).

expr 10 % 3

The terminal will output 1, proving that 3 goes into 10 three times, leaving a remainder of exactly 1.

Step 4: Using expr in Automation Scripts

The true power of expr is unlocked when you embed it inside automated bash scripts using variables. Assume you have a variable named $COUNT that currently equals 10, and you want to increase it by 1.

You must use backticks (`) or the standard $() syntax to capture the output of the expr command and assign it back to the variable.

COUNT=$(expr $COUNT + 1)

This command silently takes the current value of COUNT (10), adds 1 to it, and permanently saves the new value (11) back into the variable. By mastering the expr command, you grant your Linux scripts the foundational mathematical intelligence required to automate highly complex administrative tasks.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.