# A detailed example of mathematical operations under the Linux command line

- 2020-09-16 07:55:16
- OfStack

preface

There are several interesting commands to do math on Linux systems: expr, factor, jot, and bc commands.

Can you do math on the Linux command line? Sure! In fact, there are quite a few commands that can easily do this, and one of them may even surprise you. Let's learn these useful mathematical operation commands or command syntax.

expr

First, probably the easiest and most common command to think about doing math with commands on the command line is expr (the expression expression). It can perform four operations and can also be used to compare sizes. Here are a few examples:

variable

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

Perform simple operations

```
$ expr 11 + 123
134
$ expr 134 / 11
12
$ expr 134 - 11
123
$ expr 11 * 123
expr: syntax error <== oops!
$ expr 11 \* 123
1353
$ expr 20 % 3
2
```

Note that you need to add the \ symbol before the * operator to avoid syntax errors. The % operator is used for the remainder operation.

Here is a slightly more complicated example:

```
participants=11
total=156
share=`expr $total / $participants`
remaining=`expr $total - $participants \* $share`
echo $share
14
echo $remaining
2
```

Assuming that there are 11 participants in an event and the total number of awards to be awarded is 156, the average participant will receive 14 awards, with an additional 2 awards remaining.

To compare

Now let's look at the comparison operation. At first glance, the sentence might seem odd; Instead of setting a value, you're comparing Numbers. In this example, expr determines whether the expression is true: if the result is 1, then the expression is true; If not, the expression is false.

```
$ expr 11 = 11
1
$ expr 11 = 12
0
```

Read "Is 11 equal to 11?" "And" Does 11 equal 12?" You'll soon get used to it. Of course, we will not perform the above comparison on the command line, but a possible comparison is whether $age equals 11.

```
$ age=11
$ expr $age = 11
1
```

If you put Numbers in quotes, you will be comparing strings, not Numbers.

```
$ expr "11" = "11"
1
$ expr "eleven" = "11"
0
```

In this case, we determine whether 10 is greater than 5 and whether it is greater than 99.

```
$ expr 10 \> 5
1
$ expr 10 \> 99
0
```

Indeed, returning 1 and 0 means that the comparison is true and false, respectively, which we would normally expect to get on Linux. In the following example, using expr according to the above logic is incorrect, because if works in the opposite way, where 0 stands for true.

```
#!/bin/bash
echo -n "Cost to us> "
read cost
echo -n "Price we're asking> "
read price
if [ `expr $price \> $cost` ]; then
echo "We make money"
else
echo "Don't sell it"
fi
```

Now, let's run this script:

```
$ ./checkPrice
Cost to us> 11.50
Price we're asking> 6
We make money
```

This is clearly not what we expected! Let's modify 1 slightly so that it works as we expect:

```
#!/bin/bash
echo -n "Cost to us> "
read cost
echo -n "Price we're asking> "
read price
if [ `expr $price \> $cost` == 1 ]; then
echo "We make money"
else
echo "Don't sell it"
fi
```

factor

The factor command works pretty much as you'd expect. You give a number, and the command gives a factor for that number.

```
$ expr 11 + 123
134
$ expr 134 / 11
12
$ expr 134 - 11
123
$ expr 11 * 123
expr: syntax error <== oops!
$ expr 11 \* 123
1353
$ expr 20 % 3
2
```

0
Note: The factor command does not return more factors for the last number because 1987 is a prime number.

jot

The jot command creates a series of 1 Numbers. The total number and the starting number are given.

```
$ expr 11 + 123
134
$ expr 134 / 11
12
$ expr 134 - 11
123
$ expr 11 * 123
expr: syntax error <== oops!
$ expr 11 \* 123
1353
$ expr 20 % 3
2
```

1
You can also use jot as follows, where we want to decrease to the number 2.

```
$ expr 11 + 123
134
$ expr 134 / 11
12
$ expr 134 - 11
123
$ expr 11 * 123
expr: syntax error <== oops!
$ expr 11 \* 123
1353
$ expr 20 % 3
2
```

2
jot helps you construct lists of 1 series of Numbers that can be used for other tasks.

```
$ 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

bc is basically one of the best tools for command-line mathematics. Enter the operation you want to perform and pipe it to the command:

```
$ expr 11 + 123
134
$ expr 134 / 11
12
$ expr 134 - 11
123
$ expr 11 * 123
expr: syntax error <== oops!
$ expr 11 \* 123
1353
$ expr 20 % 3
2
```

4
You can see that bc does not ignore precision, and the input string is fairly straightforward. It can also perform size comparisons, manipulate Boolean values, calculate square roots, sines, cosines, and tangents, and so on.

```
$ expr 11 + 123
134
$ expr 134 / 11
12
$ expr 134 - 11
123
$ expr 11 * 123
expr: syntax error <== oops!
$ expr 11 \* 123
1353
$ expr 20 % 3
2
```

5
In fact, bc can even count pi. You need to specify the precision that you want.

```
$ expr 11 + 123
134
$ expr 134 / 11
12
$ expr 134 - 11
123
$ expr 11 * 123
expr: syntax error <== oops!
$ expr 11 \* 123
1353
$ expr 20 % 3
2
```

6
In addition to piping data and returning results, bc also runs interactively, typing in the calculations you want to perform. The scale setting mentioned in this example can specify the number of significant digits.

```
$ expr 11 + 123
134
$ expr 134 / 11
12
$ expr 134 - 11
123
$ expr 11 * 123
expr: syntax error <== oops!
$ expr 11 \* 123
1353
$ expr 20 % 3
2
```

7
You can also use bc for digital conversion. obase is used to set the numeric base of the output.

```
$ 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
```

bc is also one of the easiest ways to convert from base 106 to base 10 as follows:

```
$ expr 11 + 123
134
$ expr 134 / 11
12
$ expr 134 - 11
123
$ expr 11 * 123
expr: syntax error <== oops!
$ expr 11 \* 123
1353
$ expr 20 % 3
2
```

9
In the first example above, we set the input base (ibase) to 106 (hex) to complete the conversion from 106 to 10. In the second example, we do the opposite, setting the output base (obase) to base 106.

Simple bash math

By using double brackets, we can do simple math in bash. In the following example, we create a variable, assign a value to the variable, and then add, subtract, and square, in turn.

```
$ ((e=11))
$ (( e = e + 7 ))
$ echo $e
18
$ ((e--))
$ echo $e
17
$ ((e=e**2))
$ echo $e
289
```

Allowed operators include:

[
+ - Addition and subtraction

++ -- Increase and decrease

* / % Multiplication, division and remainder

^ exponential operation

You can also use logical operators and Boolean operators:

```
$ ((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
```

Or as follows:

```
$ if [ x > y ] << [ y > z ]; then echo "letters roll downhill"; fi
letters roll downhill
```

Calculate the power of 2 to the third power:

```
participants=11
total=156
share=`expr $total / $participants`
remaining=`expr $total - $participants \* $share`
echo $share
14
echo $remaining
2
```

3
conclusion

In the Linux system, there are a number of different command line tools to do the math. Hopefully, by the end of this article, you will have mastered 1 or 2 new tools.