Americas

  • United States
sandra_henrystocker
Unix Dweeb

Doing math on the Linux command line

How-To
Feb 06, 20245 mins
Linux

With the right command, you can do anything from simple math to fairly complex calculations on Linux.

Female business leader in Blockchain technology writing down a mathematical formula on glass in a library
Credit: sashalexander / Shutterstock

The Linux command line offers some very useful tools to enable you to perform mathematical calculations. This post runs through a series of commands you can use and demonstrates some techniques for doing your calculations with surprisingly little trouble.

Using expr

The most obvious command for doing math on the Linux command line is expr as in “expression”. It works with arithmetic expressions and provides nearly instant responses to your requests even when the calculations are quite complex. Here are a couple fairly simple examples. Notice the backslash that is needed to prevent the asterisk from being interpreted as a wild card.

$ expr 11 \* 11
121
$ expr 11 \* 11 \* 11
1331

Using the time command, you can measure how quickly the four-number multiplication operation below takes. It finishes in a few thousandths of a second on my modest laptop.

$ time expr 123 \* 245 \* 88 \* 314
832690320

real    0m0.003s
user    0m0.000s
sys     0m0.003s

In addition, there is no need for the backslash characters other than to preface the “wild card” character (*). The other characters that represent numerical calculations don’t require them.

$ expr 11 \* 11 - 3
118

Here are some comparisons of how these operators work.

$ expr 7 + 4
11
$ expr 7 - 4
3
$ expr 7 / 4
1
$ expr 7 % 4
3
$ expr 7 \* 4
28

That fourth operation above (%) provides the remainder for the one before (/).

You can also assign values to variables and use those variable names in your calculations as shown in the examples below.

$ num1=11
$ num2=17
$ expr $num1 + $num2
28

The expr command can also work to some extent with strings. The expressions below reports the length of the string provided, but not counting the quotes.

$ expr length "hello"
5
$ expr length "Don't spend a day without having a little fun!"
46

Comparing strings with expr

The expr command also provides a way to compare strings, but note how the longer string must be provided first.

$ expr Linux : Linux
5
$ expr LinuxNow : Linux
5
$ expr Linux : LinuxNow
0

Getting help

You can get some help on using the expr command by using the –help option (expr –help). The bulk of this will explain how the various expressions work. It includes explanations such as these:

Print the value of EXPRESSION to standard output.  A blank line below
separates increasing precedence groups.  EXPRESSION may be:

  ARG1 | ARG2       ARG1 if it is neither null nor 0, otherwise ARG2
  ARG1 & ARG2       ARG1 if neither argument is null or 0, otherwise 0
  ARG1 < ARG2       ARG1 is less than ARG2
  ARG1 <= ARG2      ARG1 is less than or equal to ARG2
  ARG1 = ARG2       ARG1 is equal to ARG2
  ARG1 != ARG2      ARG1 is unequal to ARG2
  ARG1 >= ARG2      ARG1 is greater than or equal to ARG2
  ARG1 > ARG2       ARG1 is greater than ARG2
  ARG1 + ARG2       arithmetic sum of ARG1 and ARG2
  ARG1 - ARG2       arithmetic difference of ARG1 and ARG2
  ARG1 * ARG2       arithmetic product of ARG1 and ARG2
  ARG1 / ARG2       arithmetic quotient of ARG1 divided by ARG2
  ARG1 % ARG2       arithmetic remainder of ARG1 divided by ARG2
  STRING : REGEXP   anchored pattern match of REGEXP in STRING

The commands below are examples of expr commands.

$ expr 60 / 30 \* 100
200
$ expr 20 / 30 \* 100
0

Oops! What happened in the second example above? The expr command is not going to hold onto numbers less than 0. So, you need to use some other command.

Using bc

The bc command can be persuaded to accommodate you when we need to see the decimal places. The scale value allows you to specify how many decimal places you want to see.

$ echo "scale = 2; 30 * 75 / 40" | bc
56.25
$ echo "scale = 3; 30 * 75 / 40" | bc
56.250

Using Factor

The factor command calculates the factors that multiplied will result in the original number.

$ factor 81
81: 3 3 3 3
$ factor 123
123: 3 41

Using bash commands

Note that you can also do some simple numeric calculations using bash as shown in this very basic script. Notice how the numeric comparison is enclosed in double parentheses.

#!/bin/bash

echo -n "x: "
read x
echo -n "y: "
read y

if (( x > y )); then
    echo "x > y"
else
    echo "x ! > y"
fi

You can do simple calculations like this:

$ ((z=11)
$ ((z = z + 8 ))
$ echo $z
19

Wrap-up

You can do anything from simple to fairly complex calculations on Linux. Just pick the right command to make the task easy, and be ready for a surprisingly quick response.

sandra_henrystocker
Unix Dweeb

Sandra Henry-Stocker has been administering Unix systems for more than 30 years. She describes herself as "USL" (Unix as a second language) but remembers enough English to write books and buy groceries. She lives in the mountains in Virginia where, when not working with or writing about Unix, she's chasing the bears away from her bird feeders.

The opinions expressed in this blog are those of Sandra Henry-Stocker and do not necessarily represent those of IDG Communications, Inc., its parent, subsidiary or affiliated companies.