Home / Linux / How to do math on the Linux command line

How to do math on the Linux command line

Can you do math on the Linux command line? You sure can! In fact, there are quite a few commands that can make the process easy and some you might even find interesting. Let’s look at some very useful commands and syntax for command line math.


First and probably the most obvious and commonly used command for performing mathematical calculations on the command line is the expr (expression) command. It can manage addition, subtraction, division, and multiplication. It can also be used to compare numbers. Here are some examples:

Incrementing a variable

$ count=0
$ count=`expr $count + 1`
$ echo $count

Performing a simple calculations

$ expr 11 + 123
$ expr 134 / 11
$ expr 134 - 11
$ expr 11 * 123
expr: syntax error		<== oops!
$ expr 11 * 123
$ expr 20 % 3

Notice that you have to use a character in front of * to avoid the syntax error. The % operator is for modulo calculations.

Here’s a slightly more complex example:


>> Source Link

Check Also

The Linux Foundation Will Host the Federated AI Enabler to Responsibly Advance Data Modeling

WeBank to contribute the Federated AI Enabler framework to the Linux Foundation and establish neutral …

%d bloggers like this: