Easy Tutorial
❮ Linux Comm Talk Linux Comm Mrd ❯

Linux bc Command

Linux Command Manual

The bc command is an arbitrary precision calculator language, commonly used as a calculator in Linux.

It resembles a basic calculator, allowing you to perform basic mathematical operations using this calculator.

Common Operations:

Syntax

bc(options)(parameters)

Options:

Parameters:

File: Specifies the file containing the calculation tasks.

Example

$ 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'.
2+3
5
5-2
3
2+3*1
5

Enter quit to exit.

Using Pipe

$ echo "15+5" | bc
20

Set decimal places with scale=2, where 2 means retaining two decimal places:

$ echo 'scale=2; (2.777 - 1.4744)/1' | bc
1.30

Besides scale for setting decimal places, bc also supports ibase and obase for other base operations:

$ echo "ibase=2;111" | bc
7

Base Conversion

#!/bin/bash

abc=192
echo "obase=2;$abc" | bc

Execution result: 11000000, which is converting decimal to binary using bc.

#!/bin/bash

abc=11000000
echo "obase=10;ibase=2;$abc" | bc

Execution result: 192, which is converting binary to decimal using bc.

Calculate square and square root:

$ echo "10^10" | bc
10000000000
$ echo "sqrt(100)" | bc
10

Linux Command Manual

❮ Linux Comm Talk Linux Comm Mrd ❯