## Arithmetic

Mathematical Operations in Bash

### Mathematical Operations within Bash

Bash has the capability to perform mathematical integer calculations on variables straight from the command line of from within a script. Operations such as Addition, Subtraction, Division, Multiplication, Modulus and exponentiation calculations can be performed with ease. Below is a list of operators and examples of these used within a script.

### Mathematical Operations within Bash

Bash has the capability to perform mathematical integer calculations on variables straight from the command line of from within a script. Operations such as Addition, Subtraction, Division, Multiplication, Modulus and exponentiation calculations can be performed with ease. Below is a list of operators and examples of these used within a script.

### Arithmetic Operators

Operator |
Description |
Example |
Result |
---|---|---|---|

+ | Addition | echo $(( 12 + 5 )) | 17 |

- | Subtraction | echo $(( 12 -5 )) | 7 |

/ | Division | echo $(( 27 \ 3 )) | 9 |

* | Multiplication | echo $(( 9 * 3 )) | 27 |

% | Modulo | echo $(( 5 % 3 )) | 2 |

** | Exponentiation | echo $(( 5 ** 3 )) | 125 |

### Addition

The following example illustrates how two variables $x and $y can be added together and the result assigned to the variable $answer:

**Addition Example**

**
#!/bin/bash
x=12
y=5
answer=$(echo $(( x + y )))
echo "The sum of $x and $y is : $answer"
**

**Output from above script**

**
john@john-desktop:~/scripts$ ./add.sh
The sum of 12 and 5 is : 17**

### Subtraction

The following example illustrates how variable $Y is subtracted from $x and the resulting answer is stored within the variable $answer:

**Subtraction Example**

**
#!/bin/bash
x=12
y=5
answer=$(echo $(( x - y )))
echo "$x minus $y is : $answer"
**

**Output from above script**

**
john@john-desktop:~/scripts$ ./subtract.sh
12 minus 5 is : 7
**

### Division

The following example illustrates how to divide one variable into another. In this example, $x is divided into $y with the resulting answer assigned to the variable $answer:

**Division Example**

**
#!/bin/bash
x=12
y=6
answer=$(echo $(( x / y )))
echo "$x divided by $y is : $answer"
**

**Output from above script**

**
john@john-desktop:~/scripts$ ./divide.sh
12 divided by 6 is : 2
**

### Multiplication

The following example illustrates how to multiply two variables together with the resulting answer assigned to a variable $answer:

**Multiplication Example**

**
#!/bin/bash
x=12
y=6
answer=$(echo $(( x * y )))
echo "$x multiplied by $y is : $answer"
**

**Output from above script**

**
john@john-desktop:~/scripts$ ./multiply.sh
12 multiplied by 6 is : 72
**

### Modulo

The modulo operator is generally used to find out the remainder of two numbers that are divided together. In the example below we divide the numbers 5 into 3. The answer would be 1 with a remainder of 2. This operator is often used to determine whether a number is odd or even
**Modulo Example**

**
#!/bin/bash
x=5
y=3
answer=$(echo $(( x % y )))
echo "$x mod $y leaves a remainder of : $answer"
**

**Output from above script**

**
john@john-desktop:~/scripts$ ./mod.sh
5 mod 3 leaves a remainder of : 2
**

### Interactive Script to determine if a number is Odd or Even

The following example script determines whether the number you enter is an "odd" number or an "even" number. The calculation "**remainder=$(( $number % 2 ))**" is used to check for a value of "0" which would indicate that the number entered would be an even number:

**Odd or Even Script**

**
#!/bin/bash
# Interactive Script for determining Odd or Even Numbers
#
read -p "Enter a number: " number
remainder=$(( $number % 2 ))
if [ $remainder -eq 0 ]; then
echo "The number $number you entered is an even number"
else
echo "The number you entered $number is an odd number"
fi
**

**Output from above script**

**
john@john-desktop:~/scripts$ ./mod3.sh
Enter a number: 40
The number 40 you entered is an even number
john@john-desktop:~/scripts$ ./mod3.sh
Enter a number: 41
The number you entered 41 is an odd number
**

### Exponentiation

Exponentiation is a mathematical operation involving two numbers. The first number is generally known as the base and the following number is the exponent. In the example below we raise the number "5" by the exponent of "3". This literally means "5 x 5 x 5" which would give us a value of "125":

**Exponentiation Example**

**
#!/bin/bash
x=5
y=3
answer=$(echo $(( x ** y )))
echo "$x raised by the exponent of $y equals : $answer"
**

**Output from above script**

**
john@john-desktop:~/scripts$ ./exp.sh
5 raised by the exponent of 3 equals : 125
**

### BC Bash's built in arbitrary precision calculator

Bash can not handle floating point calculations and is missing many mathematical functions. This is where the utility "**bc**" is used. "BC" can be used interactively and also within scripts to perform mathematical calculations. Numbers are arbitrary precision numbers. This precision is both in the integer part and the fractional part. These precision values can be set by the attributes of "length" and "scale". For example:

**
.000001 has a length of 6 and scale of 6.
1935.000 has a length of 7 and a scale of 3.
**

The length is the total number of significant decimal digits in a number and the scale is the total number of decimal digits after the decimal point.

An example of "scale can be seen in the following variable assignment:

**
john@john-desktop:~/scripts$ pi=$(echo "scale=10; 4*a(1)" | bc -l)
john@john-desktop:~/scripts$ echo $pi
3.1415926532
john@john-desktop:~/scripts$ pi=$(echo "scale=5; 4*a(1)" | bc -l)
john@john-desktop:~/scripts$ echo $pi
3.14156
**

### bc in a script

Although "bc" can be used interactively, we can also use its functionality within scripts by utilising the "|" pipe functionality:

**
#!/bin/bash
#
a=7
b=4
c=3
answer=$(echo "scale=2; $a * $b /$c;" | bc)
echo "The answer is $answer"
**

**Output from above "bc" script**

**
john@john-desktop:~/scripts$ ./bc1.sh
The answer is 9.33
**

### Mathematical Functions and math library

"BC" has built in functions such as square root "sqrt ( expression )", however, many other functions can be used directly from the "Math Library". This math library can be called by invoking "bc" with **bc -l"**". This causes a Math library to be preloaded with a default precision value of "20" set. The Math library has the following functions at its disposal:

**
s (x) The sine of x, x is in radians.
c (x) The cosine of x, x is in radians.
a (x) The arctangent of x, arctangent returns radians.
l (x) The natural logarithm of x.
e (x) The exponential function of raising e to the value x.
j (n,x) The Bessel function of integer order n of x.
**

### Help with BC

For a full list of functions that can be used with "bc", you can issue the command "**man bc**". Here you will find a detailed list of bc's functionality along with further examples.