# Run Math on a Linux command line using the expr command

**Linux** a terminal (shell) makes it easy to perform mathematical calculations, including addition, subtraction, multiplication, division, increment and comparison of several numbers. This lesson will show you various examples of basic mathematical calculations using **expr command**,

I followed all the commands and procedures in Debian 10, but the commands will work on any other Linux distribution.

To get started, open a terminal on your computer.

## addition

To add two or more numbers, use the expr command and the + sign as follows.

expr number 1 + number 2 + number 3 + number 4 …. number (n)

Suppose you want to add three numbers 10, 20, and 30. Do the following on your terminal.

expr 10 + 20 + 30

## Subtraction

If you want to subtract two numbers, use the minus (-) and expr commands as follows.

expr number1 – number2 – number3 … .. – number (n)

Suppose you want to subtract two numbers 30 and 20. The complete command should look like this.

expr 30 - 20

## multiplication

You can multiply any number of numbers using the expr and * operators.

The syntax of the command is as follows.

expression number 1 * number 2 * number 3

Suppose you want to multiply 5, 10, and 15. Do the following on your terminal.

expr 5 * 10 * 15

You cannot use * for multiplication here. It is used for some other purpose. If you mistakenly use it, you will get an error.

## separation

You can divide as many numbers as you like using the expr and / operators as follows.

expr number1 / number 2 / number 3 … .. number (n)

Example:

expr 50 / 5 / 2

## Increase variable

You can increase a variable by first defining and assigning a value to it. Suppose we have the variable count_var, and I assign it the value 1 as follows. Do the following on your terminal,

count_var=1

Because we defined and assigned a value to our variable. Now we can zoom as follows,

count_var= 'expr $count_var + 1'

Let’s check and print the value of the variable count_var as follows.

echo $count_var

## comparison

Using the expr and > operators, you can compare two numbers as follows.

expr number1 > number2

When the command is executed, the number 1 is compared with the number 2. If the number 1 is greater than the number 2, 1 is returned on the command line, otherwise 0 is returned.

Suppose you want to compare 20 and 10 to check if number 20 is greater than 10, do the following on your terminal.

expr 20 > 10

Result:

1

expr 10 > 20

Result:

0

You can also check if two numbers are equal using the = operator.

expr number1 = number2

If they are equal, 1 is returned to the terminal, and if they are not equal, 0 is returned.

Suppose you want to compare two numbers 10 and 10. Do the following on your terminal.

expr 10 = 10

If you want to check if two numbers are equal, you can use the operator! = As follows.

expr number1! = number2

Suppose these two numbers are 10 and 8. To check if they are equal, do the following on your terminal.

expr 10 != 8

If they are unequal, the command will return 1. If they are equal, the command will return 0.

Similarly, if you want to check, one number is equal to or less than the second number. The command should look like this:

expression number 1 <= number 2

If the first number is less than or equal to the second, the command will return 1, otherwise 0.

expr 10 <= 10

Result: 0

expr 9 <= 10

Result: 0

I covered basic math in this tutorial using the expr command. However, the jot and bc commands are left to study. You can read about them here.

Do math on the Linux command line with the expr command