How to perform basic mathematical operation in shell using bc

Scripting bc maths-in-shell-using-bc-command 538 times1 answer1 follower
0
Nisha Verma 15 Rep. Avishek Kumar edited question

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.

♥ Spread the love ♥
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
0
Avishek Kumar 449 Rep. Avishek Kumar edited answer

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

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

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

Addition in shell script using bc command

Sample Output

Substraction in shell script using bc command

Sample Output

Multiplication in shell script using bc command

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

Sample Output

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

Sample Output

Integer operation using the scale function in bc

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

Sample Output

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

Sample Output

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.

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

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

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

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

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

Boolean expression evaluation using bc

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

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

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

Functions in bc

bc supports a few functions as well.
Get the number of significant digits in an expression

Get the square root of an expression

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

♥ Spread the love ♥
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
×

Login