How to do math on the Linux command line
If we have 11 participants in some event and 156 prizes to distribute, each participant’s fair share of the take is 14, leaving 2 in the pot. Now let’s look at the logic for comparisons. These statements may look a little odd at first. They are not setting values, but only comparing the numbers. What expr is doing in the examples below is determining whether the statements are true. If the result is 1, the statement is true; otherwise, it’s false. Read them as “Does 11 equal 11?” and “Does 11 equal 12?” and you’ll get used to how this works. Of course, no one would be asking if 11 equals 11 on the command line, but they might ask if $age equals 11. If you put the numbers in quotes, you’d actually be doing a string comparison rather than a numeric one. In the following examples, we’re asking whether 10 is greater than 5 and, then, whether it’s greater than 99. Of course, having true comparisons resulting in 1 and false resulting in 0 goes against what we generally expect on Linux systems. The example below shows that using expr in this kind of context doesn’t work because if works with the opposite orientation (0=true). Now, let’s run this script: That sure isn’t going to help with sales! With a small change, this would work as we’d expect: The factor command works just like you’d probably expect. You feed it a number, and it tells you what its factors are. NOTE: The factor command didn’t get very far on factoring that last value because 1987 is a prime number. The jot command allows you to create a list of numbers. Provide it with the number of values you want to see and the number that you want to start with. You can also use jot like this. Here we’re asking it to decrease the numbers by telling it we want to stop when we get to 2: The jot command can be useful if you want to iterate through a series of numbers to create a list for some other purpose. The bc command is probably one of the best tools for doing calculations on the command line. Enter the calculation that you want performed, and pipe it to the command like this: Notice that bc doesn’t shy away from precision and that the string you need to enter is fairly straightforward. It can also make comparisons, handle Booleans, and calculate square roots, sines, cosines, tangents, etc. In fact, bc can even calculate pi. You decide how many decimal points you want to see: And bc isn’t just for receiving data through pipes and sending answers back. You can also start it interactively and enter the calculations you want it to perform. Setting the scale (as shown below) determines how many decimal places you’ll see. Related Articles See all Insider Using bc, you can also convert numbers between different bases. The obase setting determines the output base. One of the easiest ways to convert between hex and decimal is to use bc like this: In the first example above, we’re converting from hex to decimal by setting the input base (ibase) to hex (base 16). In the second, we’re doing the reverse by setting the outbut base (obase) to hex. With sets of double-parentheses, we can do some easy math in bash. In the examples below, we create a variable and give it a value and then perform addition, decrement the result, and then square the remaining value. The arithmetic operators allow you to: You can also use both logical and boolean operators: or if you prefer … Now let’s raise 2 to the 3rd power: There are sure a lot of different ways to work with numbers and perform calculations on the command line on Linux systems. I hope you picked up a new trick or two by reading this post.
participants=11
total=156
share=`expr $total / $participants`
remaining=`expr $total - $participants \* $share`
echo $share
14
echo $remaining
2
Making comparisons
$ expr 11 = 11
1
$ expr 11 = 12
0
$ age=11
$ expr $age = 11
1
$ expr "11" = "11"
1
$ expr "eleven" = "11"
0
$ expr 10 \> 5
1
$ expr 10 \> 99
0
#!/bin/bashecho -n "Cost to us> "
read cost
echo -n "Price we're asking> "
read priceif [ `expr $price \> $cost` ]; then
echo "We make money"
else
echo "Don't sell it"
fi
$ ./checkPrice
Cost to us> 11.50
Price we're asking> 6
We make money
#!/bin/bashecho -n "Cost to us> "
read cost
echo -n "Price we're asking> "
read priceif [ `expr $price \> $cost` == 1 ]; then
echo "We make money"
else
echo "Don't sell it"
fi
factor
$ factor 111
111: 3 37
$ factor 134
134: 2 67
$ factor 17894
17894: 2 23 389
$ factor 1987
1987: 1987
jot
$ jot 8 10
10
11
12
13
14
15
16
17
$ jot 8 10 2
10
9
8
7
5
4
3
2
$ for i in `jot 7 17`; do echo April $i; done
April 17
April 18
April 19
April 20
April 21
April 22
April 23
bc
$ echo "123.4+5/6-(7.89*1.234)" | bc
113.664
$ echo "sqrt(256)" | bc
16
$ echo "s(90)" | bc -l
.89399666360055789051
$ echo "scale=5; 4*a(1)" | bc -l
3.14156
$ echo "scale=10; 4*a(1)" | bc -l
3.1415926532
$ echo "scale=20; 4*a(1)" | bc -l
3.14159265358979323844
$ echo "scale=40; 4*a(1)" | bc -l
3.1415926535897932384626433832795028841968
$ bc
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
scale=2
3/4
.75
2/3
.66
quit
Invaluable tips and tricks for troubleshooting Linux
Review: RHEL 7.4 cloud-friendly, but pricey management tools
Review: Considering Oracle Linux is a no-brainer if you’re…
$ bc
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
obase=16
16 <=== entered
10 <=== response
256 <=== entered
100 <=== response
quit
$ echo "ibase=16; F2" | bc
242
$ echo "obase=16; 242" | bc
F2
Easy bash math
$ ((e=11))
$ (( e = e + 7 ))
$ echo $e
18$ ((e--))
$ echo $e
17$ ((e=e**2))
$ echo $e
289
+ - Add and subtract
++ -- Increment and decrement
* / % Multiply, divide, find remainder
^ Get exponent
$ ((x=11)); ((y=7))
$ if (( x > y )); then
> echo "x > y"
> fi
x > y
$ ((x=11)); ((y=7)); ((z=3))
$ if (( x > y )) >> (( y > z )); then
> echo "letters roll downhill"
> fi
letters roll downhill
$ if [ x > y ] << [ y > z ]; then echo "letters roll downhill"; fi
letters roll downhill
$ echo "2 ^ 3"
2 ^ 3
$ echo "2 ^ 3" | bc
8
Wrap-up
2-Minute Linux Tip: How to use the mtr command
2-Minute Linux Tips
2-Minute Linux Tip: How to use the mtr command