bc Command in Linux with Example
bc is a command line basic calculator. bc is typically used as either a mathematical scripting language or as an interactive mathematical shell.
Doing arithmetic calculation use bc as below.
bc can be used with echo command
Addition
$ echo "3+5" | bc8
Substraction
$ echo "15-3" | bc12
Multiplication
$ echo "2*8" | bc16
Division
$ echo "2/3" | bc0
$ echo "6/4" | bc
1
$ echo 'scale=25;57/43' | bc
1.3255813953488372093023255
Scale variable
The scale variable determines the number of digits which follow the decimal point in your result. By default, the value of the scalevariable is zero.Modulus Operation to find reminder
$ echo "6%4" | bc
2
2
Power
$ echo "10^3" | bc
1000
square root
$ echo 'scale=30;sqrt(2)' | bc
Calculation using bc can be done using below method also
$ bc <<< 5*4
20
$ bc <<< 5+4
9
$ bc <<< 5-4
1
$ cat bcFile
5+5
8-7
$ bc < bcFile
10
1
20
$ bc <<< 5+4
9
$ bc <<< 5-4
1
Calculation using a file
$ cat bcFile
5+5
8-7
$ bc < bcFile
10
1
Converting from binary to decimal
$ echo 'ibase=2;obase=A;10' | bc
2
convert from hexadecimal to decimal
$ echo 'ibase=16;obase=A;FF' | bc
255
No comments:
Post a Comment