Site Tools


cli:bc
$ bc <<< 5*4
20
$ bc <<< 5+4
9
$ bc <<< 5-4
1
$ echo "5*4" | bc
20
$ echo "5+4" | bc
9
$ echo "5-4" | bc
1
$ cat calcFile
5+5
6+7
 
$ bc < calcFile 
10
13

Q2. Can I use the result of last calculation within the same expression that is passed to bc?

$ echo "5-4;last+6" | bc
1
7

NOTE – You can also use a dot (.) instead of the variable name ‘last’. Here is an example :

$ echo "5-4;.+6" | bc
1
7
$ echo "sqrt(10)" | bc
3
$ echo "scale=1;sqrt(10)" | bc
3.1
$ echo "scale=10;sqrt(10)" | bc
3.1622776601

→ Convert decimal to hexadecimal

$ echo 'obase=16;128' | bc
80

→ Convert hexadecimal to decimal

$ echo 'ibase=16;obase=A;80' | bc
128

→ Convert binary to decimal

$ echo 'ibase=2;obase=A;10000000' | bc
128
cli/bc.txt · Last modified: 2021-02-23 by dcai