by

Do Math on Linux Command Line with expr command

The Linux terminal (shell) allows you to perform mathematical calculations including addition, subtraction, multiplication, division, increment, and comparison of multiple numbers easily. This tutorial will show you various examples of basic mathematical calculations using expr command.

I have performed all the commands and procedures on Debian 10, but the commands will work on any other Linux distribution as well.

To get started, open the terminal in your machine.

Addition

In order to perform the addition of two or more numbers, use the expr command and + 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. Execute the following on your terminal.

expr 10 + 20 + 30

Subtraction

If you want to perform the subtraction of two numbers, use the minus (-) and expr command as follows.

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

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

expr 30 - 20

Multiplication

You can multiply as many numbers as you like using the expr and * operator.

The syntax of the command is as follows.

expr number1 * number2 * number3

Suppose you want to multiply 5, 10, and 15. Execute 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.

Division

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

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

Example:

expr 50 / 5 / 2

Increment a Variable

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

count_var=1

Since we have defined and assigned the value to our variable. We can now increment as follows,

count_var="expr $count_var + 1"

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

echo $count_var

Comparison

With the help of expr and > operator, you can compare the two numbers as follows.

expr number1 > number2

The command, when executed, will compare the number 1 with number 2. If a number 1 is greater than number 2, it will return 1 on the command line otherwise it will return 0.

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

expr 20 > 10

Result:

1

expr 10 > 20

Result:

0

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

expr number1 = number2

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

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

expr 10 = 10

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

expr number1 != number2

Suppose, those two numbers are 10 and 8. To check if they are unequal, execute 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 or less than the second number. The command should look like,

expr number1 < = number2

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

expr 10 <= 10

Result: 0

expr 9 <= 10

Result: 0

I have covered the basic mathematical calculations in this tutorial by using expr command. However, the jot and bc commands are left to explore. You can read about them here.

Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

News Feed