Want to know how to do basic maths in command line on Linux?
The Linux command terminal is a tool that you can use to do a whole lot of tasks. Usually, when someone talks about command terminals, you think of prompts that take in some computer action-related commands. However, performing arithmetics on command terminals isn’t actually what someone usually thinks of.
Until recently, the Windows command prompt didn’t have the provision for performing arithmetic operations. Now, the command prompt has a virtual calculator to perform basic mathematical calculations.
The Linux terminal provides its users with certain commands that they can use to perform arithmetics without needing a calculator. If you use Linux and are wondering how to do basic maths in the command line on Linux, you are in the right place as we will be looking at the ways you can do exactly that.
So, let’s get going!
The $expr Command.
The $expr command or the expression command is one of the commands you can use to perform arithmetic operations. You can do addition, subtraction, multiplication, division, and compare two numbers using the
$expr command. Let’s have a look at how you can use the $expr command to perform the above-mentioned operations.
For adding two numbers in the Linux command terminal, you need to use the following syntax.
$ expr number1 + number2
$ expr 100 + 100
You can use similar syntax to add more than two numbers.
$ expr number1 + number2 + number3…..
$ expr 100 + 100 + 100
This is how you can do addition on the command terminal.
For subtracting two numbers, you can use the same syntax used for addition. You just have to use “-” instead of “+”.
$ expr number1 - number2
$ expr 100 - 90
Similarly, use the same syntax as used in addition to subtracting two or more numbers.
$ expr number1 - number2 - number3…..
$ expr 150 - 70 -10
Again, the syntax for multiplying two numbers in the command terminal is quite similar to the syntax for addition and subtraction.
$ expr number1 \* number2
$ expr 2 \* 2
For multiplying two or more numbers, use the following syntax.
$ expr number1 \* number2 \* number3….
$ expr 2 \* 3 \* 4
You might be wondering why we don’t use the asterisk (*) only to specify multiplication. This is because the asterisk is used to point out the current directory.
You can divide two numbers in the command line using the following syntax.
$ expr number1 / number 2
For example, 100 / 10
Comparing Two Numbers.
You can also use the
$expr command to compare two numbers. When comparing using, the expression becomes boolean, so the answer you get as the output doesn’t specify which number is greater. Rather, it gives a 1 or a 0 depending on whether the input expression is true or false. The syntax for comparing numbers is:
$ expr number1 \> number2
$ expr number1 \< number2
$ expr 15 \> 20
You can also use the equal “=” sign with the
$expr command. This also becomes a boolean expression, and you get a 1 or 0 depending on the input expression. Using the equality sign can prove useful when you are specifying and checking a condition. All the boolean expressions formed with
$expr can be used to check conditions on the terminal.
$ expr number1 = number2
$ expr 10=10
The $factor Command.
The $factor command, as the name suggests, is used to find factors of the number we provide as the input. The syntax that is used with
$factor is as follows.
$ factor number
$ factor 6
$ factor 23
As the number 23 is a prime number, it doesn’t have factors other than itself. So you only see 23 as the output.
The $bc Command.
$bc command is a powerful tool that can be used to perform a variety of calculations related to mathematics on the command terminal.
It can perform complex calculations that involve DMAS and also handle boolean expressions along with performing calculations on square roots, sines, cosines, and tangents. Overall, it is quite a handy tool. Here is its syntax.
$ echo “mathematical expression” | bc
$ echo “14+20-6/2” | bc
$ echo “20*20/20*20” | bc
The $jot Command.
$jot command is another helpful tool that can help you make lists of numbers. How it works is that you specify a number, and then you can see the numbers coming after or before it.
The real value of the
$jot command comes when you want to use the for loop while working on the command terminal. The
$jot command can be used as the incrementing or decrementing variable/value. Following are some examples of the
$ sudo apt install athena-jot
$ jot 10 1
In this example, you will get 10 numbers printed after 1. As seen in the image below.
$ jot 10 20 10
In this example, you will get 10 numbers coming before 20 printed in reverse. As seen in the image below.
$ for i in ‘jot x y’; do …..
This is an example of how you can use $jot in combination with loop. The loop will work until the value x increments to the value of y. This is how to do basic math in the command terminal on Linux.
As you may know now, the Linux command terminal is an excellent application. You can perform endless tasks on it, including rebooting the operating system.
Sometimes the operating system stops working, and you become stuck. The system becomes unresponsive, slow, or doesn’t work as you want it to. In times like these, rebooting the computer is the best step.
If you want to know how to reboot the system using the command terminal, go to this link. It will lead you to a guide that gives you a detailed answer to your question.
In this article, we looked at how to do basic math in the command terminal on Linux. There are various commands available for arithmetic operations and other operations as well. We hope that we were able to help you out in understanding each command.
If this guide helped you, please share it 🙂