{"id":1912,"date":"2022-03-04T22:12:24","date_gmt":"2022-03-04T16:42:24","guid":{"rendered":"https:\/\/smarttech101.com\/?p=1912"},"modified":"2023-03-25T00:52:09","modified_gmt":"2023-03-24T19:22:09","slug":"bc-command-in-linux","status":"publish","type":"post","link":"https:\/\/smarttech101.com\/bc-command-in-linux\/","title":{"rendered":"bc command in Linux"},"content":{"rendered":"\n
bc command in Linux is actually a calculator “language”. It comes preinstalled with Linux. Like bash and zsh, you can use it in interactive mode (i.e. type bc in your terminal, hit Enter button, put your equations and get your answers) as well as in shell scripts.<\/p>\n\n\n\n
Some people like to call it the “Basic Calculator<\/strong>” and discard it altogether. But I call it the “Best Calculator”<\/strong> because it provides advanced facilities as well – It has Here, in this article, I will touch upon its basic applications and how I use it in the shell scripts and in its interactive mode.<\/p>\n\n\n\n To enter into bc’s interactive mode, simply use the command bc in your terminal.<\/p>\n\n\n\n You can also force the bc command into the interactive mode using the flag Quit: <\/strong>To quit from the bc’s interactive mode, use the command By default, bc sets the scale to 0. That means – zero number of digits will be printed out after the decimal point. In other words, only the integer part is printed. To set it to some non-zero value use This is one of the features not available in GUI-based calculators. You can assign any value to any variable. <\/p>\n\n\n\n For the following example, I am assigning value The To remove all kinds of information from bc, you need to use the bc command has For example, to find the decimal number corresponding to hexadecimal number ‘E’, you need to define obase as 10 and ibase as 16.<\/p>\n\n\n\n \u26a0\ufe0f Be Careful<\/strong>: if you set ibase first and then obase, you will get unexpected answers:<\/p>\n\n\n\n The reason is that the first line’s ibase=16 asks the bc to treat all the upcoming numbers as hexadecimal numbers. Therefore, the 10 in the next line To use these functions, you need to load the math library using the flag At the same time, it also sets the default scale to 20 as you can in the following example:<\/p>\n\n\n\n Similar to C language, bc has its own conditional statements ( Please look at the man page to know more.<\/p>\n\n\n\n You can put all the commands, which you are going to type in the interactive mode, into a file and then execute the bc command on that file.<\/p>\n\n\n\n Now, the bc will first execute all lines one by one starting from the beginning in the FILE. After executing these lines, bc will read from the standard input. If there is no standard input, bc will enter into the interactive mode.<\/p>\n\n\n\n Example:<\/strong><\/p>\n\n\n\n \ud83d\udcdd Note 1:<\/strong> As you can see in the above example, \ud83d\udcdd Note 2:<\/strong> \ud83d\udcdd Note 3: <\/strong>You can also use multiple files containing your bc-commands.<\/p>\n\n\n\n \ud83d\udcdd Note 4: <\/strong>Use the command \ud83d\udc68\u200d\ud83d\udd27 Application: <\/strong>Some of you complain that the GUI-based calculators have extra facilities which are not available in bc like unit and currency conversions. In bc, you can easily add new functionalities<\/strong> by loading FILE(s) at the start. For example, to create a factorial function ( Now, In order to use bc in shell scripts, just pipe all the commands, which you would have typed in its interactive mode, into the bc.<\/p>\n\n\n\n Learn here about\u00a0what is a shell script and how to create one.<\/a><\/p>\n\n\n\n For instance, using the echo command<\/a>, I will be executing the following command:-<\/p>\n\n\n\nfor<\/code> and
while<\/code> loops, and if-else statements, among others with syntax similar to the C programming language.<\/p>\n\n\n\n
Table of contents<\/h2>\n\n\n\n
\n
\n
Why bc is the best calculator in Linux<\/h2>\n\n\n\n
\n
\n
read<\/code> similar to that in the C language. So you can exploit your talent in that regard as well and do complex calculations.<\/li>\n\n\n\n
<\/li>\n<\/ul>\n\n\n\npaste -sd '+' \"$rxdeltafile\" | bc<\/code><\/figcaption><\/figure>\n\n\n\n
\n
bash<\/code> or
zsh<\/code>, you can use their Arithmatic Evaluation functionality i.e.
((expression))<\/code>. But in strictly POSIX compliant shells such as
dash<\/code>, there is nothing like
((expression))<\/code> and hence, you don’t have any option other than the
bc<\/code>. <\/li>\n\n\n\n
echo $((5+5.1))<\/code> throws error in bash) . So in this case, bc is a life saver.<\/li>\n<\/ul>\n\n\n\n
How to use bc command interactively<\/h2>\n\n\n\n
[ajay@lenovo ~]$ bc\nbc 1.07.1\nCopyright 1991-1994, 1997, 1998, 2000, 2004, 2006, 2008, 2012-2017 Free Software Foundation, Inc.\nThis is free software with ABSOLUTELY NO WARRANTY.\nFor details type `warranty'.<\/code><\/pre>\n\n\n\n
-i<\/code> or
--interactive<\/code>.<\/p>\n\n\n\n
quit<\/code>, or shortcut key
ctrl + \\<\/code> (the SIGQUIT), or
ctrl + c<\/code> (the SIGINT) or
ctrl + d<\/code>.<\/p>\n\n\n\n
How to set the scale (outputs in decimal point) in bc<\/h2>\n\n\n\n
scale=n<\/code> as shown below:<\/p>\n\n\n\n
[ajay@lenovo ~]$ bc\nbc 1.07.1\nCopyright 1991-1994, 1997, 1998, 2000, 2004, 2006, 2008, 2012-2017 Free Software Foundation, Inc.\nThis is free software with ABSOLUTELY NO WARRANTY.\nFor details type `warranty'. \nscale=3\n5\/4\n1.250<\/code><\/pre>\n\n\n\n
Variables and values in bc<\/h2>\n\n\n\n
3<\/code> to a variable
var<\/code>. Now, you can use this value in the future!!! Then the
print<\/code> command prints any variable’s value. <\/p>\n\n\n\n
[ajay@lenovo ~]$ bc\nbc 1.07.1\nCopyright 1991-1994, 1997, 1998, 2000, 2004, 2006, 2008, 2012-2017 Free Software Foundation, Inc.\nThis is free software with ABSOLUTELY NO WARRANTY.\nFor details type `warranty'.\nvar=3\nvar1=var*2\nprint var1\n6<\/code><\/pre>\n\n\n\n
Previous answer in bc using the “last” variable<\/h3>\n\n\n\n
last<\/code> variable is a predefined variable and it contains the last answer. Using this, you can concatenate your answers and solve a long equation. Without the
last<\/code> variable, you will need to constantly copy and paste the answers. For example, to solve 3+8789\/3*6*9 step by step:<\/p>\n\n\n\n
[ajay@lenovo ~]$ bc\nbc 1.07.1\nCopyright 1991-1994, 1997, 1998, 2000, 2004, 2006, 2008, 2012-2017 Free Software Foundation, Inc.\nThis is free software with ABSOLUTELY NO WARRANTY.\nFor details type `warranty'.\nscale=10\n8789\/3\n2929.6666666666\nlast*6\n17577.9999999996\nlast*9\n158201.9999999964\n3+last\n158204.9999999964<\/code><\/pre>\n\n\n\n
How to remove the warranty and other information from the bc command in Linux<\/h2>\n\n\n\n
--quiet<\/code> or
-q<\/code> flag.<\/p>\n\n\n\n
[ajay@lenovo ~]$ bc --quiet\nscale=3\n5\/4\n1.250<\/code><\/pre>\n\n\n\n
How to use the bc command in Linux to convert a number from one base to another<\/h2>\n\n\n\n
ibase<\/code> and
obase<\/code> variables for input and output bases respectively.<\/p>\n\n\n\n
[ajay@lenovo ~]$ bc --quiet\nobase=10\nibase=16\nE\n14<\/code><\/pre>\n\n\n\n
[ajay@lenovo ~]$ bc --quiet\nibase=16\nobase=10\nE\nE<\/code><\/pre>\n\n\n\n
obase=10<\/code> becomes 16. Hence, the second line gets transformed into
obase=16<\/code>. And that’s why E is printed as E.<\/p>\n\n\n\n
Trigonometric, logarithmic, and exponential functions in bc command in Linux<\/h2>\n\n\n\n
--mathlib<\/code> or
-l<\/code>. Here is a list of available functions in the math library and their descriptions:<\/p>\n\n\n\n
\n
[ajay@lenovo ~]$ bc --quiet --mathlib\npi=4*a(1)\ns(pi)\n.00000000000000000002<\/code><\/pre>\n\n\n\n
Advanced bc commands in Linux<\/h2>\n\n\n\n
if-else<\/code>), loops (
for<\/code> and
while<\/code>),
break<\/code> and
continue<\/code>,
return<\/code>, comparison operators (
>=, <=, <, >, ==, !=<\/code>), boolean operators (
!, ||, &&<\/code>),
++var, var++, --var, var--<\/code>,
read<\/code>,
return<\/code> and other functions.<\/p>\n\n\n\n
Executing bc command on a file containing all the equations<\/h2>\n\n\n\n
[ajay@lenovo ~]$ bc FILE<\/code><\/pre>\n\n\n\n
[ajay@lenovo ~]$ cat equation.txt\nprint \"remainder of 5\/4 is \"\n5%4\n\/* this is a comment *\/\nlast+2\n\n[ajay@lenovo ~]$ bc equation.txt \nbc 1.07.1\nCopyright 1991-1994, 1997, 1998, 2000, 2004, 2006, 2008, 2012-2017 Free Software Foundation, Inc.\nThis is free software with ABSOLUTELY NO WARRANTY.\nFor details type `warranty'. \nremainder of 5\/4 is 1\n3\n^C\n(interrupt) Exiting bc.\n\n[ajay@lenovo ~]$ echo 'print \"standard input\\n\"' | bc equation.txt\nremainder of 5\/4 is 1\n3\nstandard input\n\n[ajay@lenovo ~]$<\/code><\/pre>\n\n\n\n
\/* *\/<\/code> is used for comment, and
%<\/code> is used for the remainder.<\/p>\n\n\n\n
^C<\/code> is ctrl+c I pressed to exit from the interactive mode.<\/p>\n\n\n\n
[ajay@lenovo ~]$ bc FILE1 FILE2<\/code><\/pre>\n\n\n\n
quit<\/code> at the end of the
FILE<\/code> to prevent bc from entering into the interactive mode.<\/p>\n\n\n\n
n!<\/code>), create a file
factorial.bc<\/code> with the following contents in it:<\/p>\n\n\n\n
define f (x) {\n if (x <= 1) return (1);\n return (f(x-1) * x);\n }<\/code><\/pre>\n\n\n\n
5!<\/code> gives
5*4*3*2*1=120<\/code> as given below:<\/p>\n\n\n\n
[ajay@lenovo ~]$ bc --quiet factorial.bc \nf(5)\n120<\/code><\/pre>\n\n\n\n
How to use bc command in shell scripts<\/h2>\n\n\n\n
[ajay@lenovo ~]$ echo 'scale=3; 5\/4' | bc\n1.250<\/code><\/pre>\n\n\n\n