# How to perform basic mathematical operation in shell using bc

Very recently I came to know about an arbitrary precision calculator language called **bc**, which supports arbitrary precision numbers along with interactive execution of statements. I am curious to know about its syntax for basic mathematical operations. Also, how it can be used in shell scripting language to yield desired output.

**bc** is designed to be interactive, hence the statement and expressions are executed as soon as it is run. Here are the examples to understand it thoroughly.

### Run Linux command-line utility bc in interactive mode

Go to console/terminal and run command **bc**

1 2 3 4 5 6 |
$ 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'. |

Now you may run basic mathematical expression in interactive mode and get result interactively,

1 2 3 4 5 6 7 8 |
2+2 4 2-3 -1 6/3 2 2*7 14 |

### Execute mathematical expression using command ‘bc’, from a shell script.

##### Addition in shell script using bc command

1 2 3 4 5 6 7 8 |
#!/bin/bash echo -n "Enter First Number and press [ENTER]: " read first_number echo -n "Enter Second Number and press [Enter]: " read second_number result=$(echo $first_number+$second_number | bc) printf "Sum of $first_number and $second_number is $result\n" |

Sample Output

1 |
Sum of 5 and 4 is 9 |

##### Substraction in shell script using bc command

1 2 3 4 5 6 |
echo -n "Enter First Number and press [ENTER]: " read first_number echo -n "Enter Second Number and press [Enter]: " read second_number result=$(echo $first_number-$second_number | bc) printf "$second_number - $first_number = $result\n" |

Sample Output

1 |
7 - 12 = 5 |

##### Multiplication in shell script using bc command

1 2 3 4 5 6 7 8 9 10 11 |
#!/bin/bash echo -n "Enter First Number and press [ENTER]: " read first_number echo -n "Enter Second Number and press [Enter]: " read second_number result=$(echo $first_number*$second_number | bc) printf "$first_number * $second_number = $result\n" <pre> Sample Output <pre>4 * 3 = 12 |

**Note: **Running “echo 12*3 | bc” directly on the terminal works, but running “echo 12 * 3 | bc” directly on terminal won’t. You need to add an escape sequence(\) like the example here “echo 12 \* 3 | bc”.

##### Division in shell script using bc command

1 2 3 4 5 6 7 8 |
#!/bin/bash echo -n "Enter First Number and press [ENTER]: " read first_number echo -n "Enter Second Number and press [Enter]: " read second_number result=$(echo $first_number/$second_number | bc) printf "$first_number / $second_number = $result\n" |

Sample Output

1 |
18 / 2 = 9 |

As illustrated in the examples above, you can perform the mathematical operation on any count of numbers. Also, **bc** support operation on integers.

##### Just another way of expression evaluation using bc

1 |
bc <<< "3/2" |

Sample Output

1 |
1 |

### Integer operation using the scale function in bc

If we define scale, we can get the desired count of numbers after decimal.

1 |
echo "scale=2;(3/2)" | bc |

Sample Output

1 |
1.50 |

Output value of 22/7 upto 25 digit post decimal.

1 |
$ echo "scale=25;(22/7)" | bc |

Sample Output

1 |
3.1428571428571428571428571 |

Don’t confuse, if you find the above expression **bc <<< "3/2"** different. It can also be written as echo **3/2″ | bc.** All examples in this tutorial can be rewritten as above. It is just a matter of personal choice.

### Relational expression evaluation using bc

Other than that, you can also execute relational expressions. Relational expressions are those mathematical expressions which strictly evaluate to 1 (true) or 0(false).

##### Examples of relational expression evaluation using bc

**expr1 < expr2 : The result is 1 if expr1 is strictly less than expr2.**

1 2 3 4 5 |
echo "(7+3) < 8" | bc 0 echo "(2+3) < 8" | bc 1 |

**expr1 <= expr2 : The result is 1 if expr1 is less than or equal to expr2.**

1 2 3 4 5 |
echo "(4+3) <= (2+3)" | bc 0 echo "(2+3+5) <= (8*2)" | bc 1 |

**expr1 > expr2 : The result is 1 if expr1 is strictly greater than expr2.**

1 2 3 4 5 |
echo "(9/3) > (2*2)" | bc 0 echo "(2+2) > (2*1)" | bc 1 |

**expr1 >= expr2 : The result is 1 if expr1 is greater than or equal to expr2.**

1 2 3 4 5 |
echo "(12-9) >= (3*2)" | bc 0 echo "(2+2) >= (2*2)" | bc 1 |

**expr1 == expr2 : The result is 1 if expr1 is equal to expr2.**

1 2 3 4 5 |
echo "(7*2) == (10+4+1)" | bc 0 echo "(7*2) == (10+4)" | bc 1 |

**expr1 != expr2 : The result is 1 if expr1 is not equal to expr2.**

1 2 3 4 5 |
echo "(5*2) != (5+5)" | bc 0 echo "(3-1) != (5+5)" | bc 1 |

### Boolean expression evaluation using bc

**expr && expr : The result is 1 if both expressions are non-zero.**

1 2 3 4 5 |
echo "(1+1) && (0/2)" | bc 0 echo "(1+1) && (2+2)" | bc 1 |

**expr || expr : The result is 1 if either expression is non-zero.**

1 2 3 4 5 |
echo "(0/1) || (0/2)" | bc 0 echo "(1+1) || (0/2)" | bc 1 |

**!expr : The result is 1 if expr is 0.**

1 2 3 4 5 |
echo "! (2+3)" | bc 0 echo "! (3-3)" | bc 1 |

### Functions in bc

bc supports a few functions as well.

**Get the number of significant digits in an expression**

1 2 3 4 5 |
echo "length (21240)" | bc 5 echo "length (5+2*10)" | bc 2 |

**Get the square root of an expression**

1 2 |
echo "sqrt (16)" | bc 4 |

There are a lot more things that **bc** supports. Just run **man bc** in your terminal and see what new you find there.

*×*