How To Use GNU Basic Calculator (Bc) In Linux For Math Calculation?

GNU Basic Calculator

Almost all POSIX-compliant operating system comes with decades-old GNU bc utility by default. GNU bc (Basic Calculator) is an arbitrary precision mathematical scripting language.

Bc has syntax similar to the C programming language. Not just syntax, Bc also provides features that you find in every other programming language.

For instance, control statements using if/else, iterative statements using for or while loop, math functions, conditional statements, and different types of operators like arithmetic, logical, and assignment.

Basic Calculator (bc)
Basic Calculator (bc)

You can use the bc utility to perform basic and advanced mathematical calculations in your shell scripts using the echo command.

Use bc in shell script
Use bc in shell script

Interactive Mode In Bc

Interestingly, basic calculator also provides an interactive mathematical shell to execute operations. By just typing ‘bc’ in your terminal, you can get into an interactive mode and test all available functions and expressions.

Bc interactive mode
Basic calculator interactive mode

Advanced Bc Functions Using mathlib

For doing advanced math calculation using sine, cosine, and logarithm functions, bc provides a standard math library. It can be used by loading it using a command-line option --mathlib or -l with bc.

Currently, the mathlib supports the following functions :

  • s (x) for sine
  • c (x) for cosine
  • a (x) for arctangent
  • l (x) for natural logarithm
  • e (x) for the exponential function of raising e to the value x
  • j (n,x) for the bessel function of integer order n of x
  • sqrt(x) for square root
bc built-in functions
basic calculator built-in functions

Additionally, mathlib also supports the following special functions:

  • length(x) for the number of digits in x
  • read() to read the number from the standard input
  • scale(expression) for the number of digits after the decimal point in the expression
  • ibase and obase for the conversion base for input and output numbers
  • last (an extension) that holds the value of the last printed number

Make Custom Bc Functions

Along with built-in functions, basic calculator also lets you create your own functions as per your need. To make a custom function, you need to start it with define keyword using the following syntax:

define function_name ( parameters ) {
    statement...
    return statement;
}
Define own bc function

Convert Binary To Decimal And Decimal To Binary Using Bc

Using the ibase and obase variable, you can also convert binary to decimal and vice-versa.

To convert binary to decimal:

$ echo 'ibase=2;obase=A;11' | bc -l
binary to decimal using bc
binary to decimal using basic calculator

To convert decimal to binary:

$ echo 'ibase=10;obase=2;3' | bc
decimal to binary using bc
decimal to binary using basic calculator

Take Input From A File

Instead of typing expressions each time, you can also put your all math expression in a file and execute it using the bc command.

Take input from file

Similar Posts