
Linux provides quite a few commands that let you do math on the command line. Which is best depends on the kind of math that you’re doing and the precision that you’re expecting. This post covers five different commands with examples of how to use them and expands upon my earlier post.
Using expr
The expr command is one of the most frequently used commands for doing integer arithmetic. Here are some examples for adding subtracting and dividing:
$ expr 77 + 55
132
$ expr 77 – 22
55
$ expr 77 / 11
7
$ expr 77 * 2
154
To store the result of a calculation in a variable, just do something like this:
$ total=`expr 77 + 55`
$ echo $total
132
To multiply 52 times 7 (to estimate the days in a year), you could use a command like this:
$ expr 52 * 7
364
One important thing to keep in mind when using the expr command is the order of precedence of the numeric operators. If your expression includes only + and -, the calculation will proceed from left to right. But, if you’re also including a * (multiplication) or / (division) along with an addition or subtraction as in the examples below, the multiplication and divisions take precedence.
$ expr 8 + 77 / 11
15
$ expr 12 + 6 * 3
30
As you can see from the results displayed above, the 77 / 11 operation is done first and then 8 is added in the first example. In the second example, the 6 is multiplied by 3 before 12 is added.
Using double-parens
Another easy way to do integer math is to use double parentheses as in the examples below. As with the expr command, multiplications and divisions are done prior to additions and subtractions.
$ echo $((3 + 4))
7
$ echo $((10 * 5))
50
$ echo $((24 / 3))
8
$ echo $((10 * 5 + 1))
51
$ echo $((1 + 5 * 10))
51
$ echo $((1 + 10 / 5))
3
Keep in mind that the double-parens method only supports integers.
$ echo $((10 / 2.5))
-bash: 10 / 2.5: syntax error
Using the bc command
The bc command is used for more precision calculations. While it is a language in itself, it is frequently used for floating point mathematical operations as in the examples below.
$ echo "12+5" | bc
17
$ echo "10^2" | bc
100
$ echo "3 + 4" | bc
7
$ echo "scale=2; 10 / 3" | bc
3.33
$ echo 11.11 + 4.3 | bc
15.41
The scale argument allows you to set the decimal precision that you want.
The bc command also supports more advanced math with the -l (math library) option that handles numbers as floating point objects:
$ echo 100/3 | bc
33
$ echo 100/3 | bc -l
33.33333333333333333333
Using awk
The awk command is easy to use and works for both integer and floating-point math. Here are a few examples:
$ awk 'BEGIN { print 3 + 4 }'
7
$ awk 'BEGIN { print 10 / 3 }'
3.33333
$ awk 'BEGIN { x = 5; y = 7; z = x + y; print z }'
12
Using python
While a versatile scripting language, python can also be used for complex and scriptable math on the command line as in the examples below.
$ python3 -c "print(10 / 3)"
3.3333333333333335
$ python3 -c "import math; print(math.sqrt(16))"
4.0
Using scripts
You can put your calculations into scripts like the one below so that you can easily run them again and again. Note that this script uses the bc command to provide answers with three different numbers of decimal places and loops until you ask it to exit.
#!/bin/bash
echo "Do some math with bc (or type 'exit' to quit)"
while true; do
read -p "Enter expression: " input
if [[ "$input" == "exit" ]]; then
break
fi
echo "Result: $(echo "$input" | bc -l)"
echo "Result: $(echo "scale=5; $input" | bc -l)"
echo "Result: $(echo "scale=0; $input" | bc -l)"
done
Wrap-up
Doing math on the Linux command line can be quite easy once you get used to the particular commands that can perform the calculations to best suit your needs.
Source:: Network World