Home » linux » Linux and Unix bc command tutorial with examples

Linux and Unix bc command tutorial with examples

bc command is used to perform the basic calculation.

Example:

To add two number

write “bc” command on terminal and press enter then specify two number as mention below for addition.

Note: bc command print the result of computation in next line as shown above.

Perform multiple calculations: we can perform multiple calculations on the same line by using “; ”delimiter in bc command.
As shown below we perform three calculations (multiplication, subtraction, power) each separated by ; as the delimiter.

Note: Result of each calculation display in the separate line as shown above. ^ indicates “to the power of”.

bc command performs only integer computation: bc command can perform integer computation and if decimal portion comes in computation it truncates decimal portion. For example:

For decimal calculation by using bc command: for the decimal calculation we have to set “scale” to the number of digits of precision as shown below. We must set the scale before calculation.

Managed by MochaHost - Fast & Easy WordPress Hosting