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.
1 2 3 4 | $ bc [press enter] 22+5 [number enter by user at terminal] 27 [result display after computation in next line ] [ctrl+d] [used to quit bc command] |
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.
1 2 3 4 5 6 | $ bc [press enter] 2*5 ; 44-5 ; 2^3 [number enter by user at terminal] 10 [result of multiplication] 39 [result of subtraction] 8 [result of power] [ctrl+d] [used to quit bc command] |
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:
1 2 3 4 | $ bc [press enter] 23/3 [number enter by user at terminal] 7 [result: decimal portion truncates ] [ctrl+d] [used to quit bc command] |
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.
1 2 3 4 5 | $ bc [press enter] Scale=2 5/4 [number enter by user at terminal] 1.25 [result with decimal value ] [ctrl+d] [used to quit bc command] |