bc command in Unix | Linux Tutorial


bc command in Unix

bc command is used to perform calculations.
Press Ctrl + D to exit bc.


bc
15+4

19
[Ctrl + D]

We can perform multiple calculations on a single statement with the use of a semicolon, but the outputs will be displayed on different lines.

bc
12*2 ; 2^3

24
8

bc only performs integer calculations and truncates the decimal points, as you can see

bc
23/5

4

To enable floating-point conversion, you have to set the scale to the number of digits you of precision before the statement

bc
scale=2
21/4

5.25

bc also has some inbuilt functions, like
bc
sqrt(64)

8

We can also declare variables in bc.
bc
x=10
y=15
x+y

25

bc is quite useful to convert the bases. For this we use ibase and obase.
ibase specifies the input base and obase specifies the output base.
Default is Decimal.

bc
ibase=2
11001010

202

bc
obase=2
14

1110

Comments