Americas

  • United States
sandra_henrystocker
Unix Dweeb

Numeric operations on Linux

How-To
Oct 23, 20234 mins
Linux

Linux offers a lot of helpful commands for doing math on the command line.

Linux systems provide numerous ways to work with numbers on the command line – from doing calculations to using commands that generate a range of numbers. This post details some of the more helpful commands and how they work.

The expr command

One of the most commonly used commands for doing calculations on Linux is expr. This command lets you use your terminal window as a calculator and to write scripts that include calculations of various types. Here are some examples:

$ expr 10 + 11 + 12
33
$ expr 99 - 102
-3
$ expr 7 * 21
147

Notice that the multiplication symbol  * in the command above requires a backslash to ensure the symbol isn’t interpreted as a wildcard. Here are some more examples:

$ expr 147 / 7
21
$ expr 81 % 10
1

The two calculations above do a division and provide a modulus (the number that remains after 81 is divided by 10).

Using double parentheses

You can also do a lot of calculations using double parentheses. Here’s an example:

$ sum=$(( 1 + 10 ))
$ echo $sum
11

Other calculations using double parentheses (multiplication, subtraction, division and modulus) are shown below.

$ num=$(( 3 * 17 ))
$ echo $num
51
$ num=$(( 124 - 37 ))
$ echo $num
87
$ num=$(( 124 / 37 ))
$ echo $num
3
$ num=$(( 124 % 37 ))
$ echo $num
13

You can also combine parentheses and expr commands like this:

$ num=5
$ num=$(expr $num + 11)
$ echo $num
16

Alternately, you can use backticks like this:

$ num=5
$ num=`expr $num + 11`
$ echo $num
16

Using bc

The bc command provides a precision calculator that allows you to do calculations and even specify how many decimal places you want to see when the results are not whole numbers.

$ echo "26/3" | bc
8
$ echo "scale=3;26/3" | bc
8.666

You can also calculate factorials with commands like the one shown below. Just in case you haven’t thought about factorials in many years, they involve multiplying all whole numbers from the chosen number down to one. For example, 3! (factorial 3) would be 6 (3 x 2 x 1). The command below calculates the value of 10! by using the seq command to generate the list of numbers (10 9 8 etc.) and then passing them to bc to do the multiplication.

$ seq -s "*" 1 10 | bc
3628800
$ expr 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1
3628800

I included the expr command to verify that the seq output sent to the bc command is generating the correct output.

Using awk

Even awk can help with math. Here are two examples of commands you can try on the command line:

$ awk 'BEGIN { a = 76; b = 42; print "(a + b) =", (a + b) }'
(a + b) = 118
$ echo 66 77 90 | awk '{ sum = $1 + $2 + $3 ; avg = sum /3; print avg }'
77.6667

Using the factor command

The factor command is used to print the prime factors of given numbers. Here’s an example in which the number 111 is factored:

$ factor 111
111: 3 37

As you can see, the two factors for 111 are 3 and 37. You can verify this if you’re so inclined by multiplying those numbers and seeing that the result is indeed 111.

$ expr 3 * 37
111

If you make the starting number one larger, you will see a much longer list of factors:

$ factor 112
112: 2 2 2 2 7

Clearly the multipliers required to get to 112 are much more significant. Remember that all of the factors need to be prime numbers or we would be seeing just 16 and 17.

$ expr 2 * 2 * 2 * 2 * 7
112

Numeric operators

The table below displays the numeric operators that you can use with most numeric commands. Just remember to use a backslash before an *.

operator operation
+ addition
subtraction
/ division
* multiplication
% modulus

Numeric comparisons

You can also compare numbers with operations that test whether numeric variables are equal, unequal, greater or smaller than some other variables. Here’s one example:

$ num=11
$ if [ $num == 11 ]; then echo eleven; else echo oops; fi
eleven

To test whether numbers are equal to, greater or equal to or equal to come value, use a command like one of these:

$ if [ $num -ge 11 ]; then echo ok; else echo oops; fi
ok
$ if [ $num -gt 11 ]; then echo wow; else echo ok; fi
ok
$ if [ $num -lt 9 ]; then echo wow; else echo ok; fi
ok
$ if [ $num != 11 ]; then echo wow; else echo ok; fi
ok

The table below lists the numeric comparisons.

operator test
== equal
-ge greater or equal
-gt greater than
-lt less than
!= not equal

Generating number lists

You can generate lists of numbers using the jot or seq commands (depending on which version of Linux you are using).

$ jot 5
1
2
3
4
5
$ seq 1 5
1
2
3
4
5

In fact, there’s no requirement that you start with 1.

$ seq 27 33
27
28
29
30
31
32
33

You also don’t have to list every number in the specified range. The seq command below starts with 10, increments each number by 4 and then stops when it reaches the last increment in the range (24 in this case).

$ seq 10 4 24
10
14
18
22

Wrap-up

Doing math on the command line can be fun and often quite helpful when you need to calculate values. If you’d like to see a post I wrote five or so years ago on doing math on the command, check this one out:

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.