bc is a basic calculator in Linux. Even though it is very simple, it is also very powerful because it allows for arbitrary precision arithmetic.
This tutorial is interactive, you are encouraged to type everything in while reading it!
To start bc, type this into the shell:
Yes, it is that easy. You should receive a message like this:Code:bc
Now you can type in simple calculations. Try some of these:Code:bc 1.06.94 Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc. This is free software with ABSOLUTELY NO WARRANTY. For details type `warranty'.
You output should be like this:Code:1+1 6*7 8^3 1+2*3 (1+3)*3 2/3
bc does order of operations, just like you would expect.Code:2 42 512 7 12 0
Everything looks fine, except 2/3 equals 0. Why did this happen?
By default bc does not deal with decimal places. If you want bc to show decimal places, you need to set the scale. Type this into bc:
scale is a variable in bc. When you type that into bc, you make scale equal to ten. Now type this:Code:scale=10
bc should output 10, because that is what scale equals now. Try this:Code:scale
Now you should get this as an output:Code:2/3
bc calculated 2/3 correctly to ten decimal places.Code:.6666666666
Now type this in:
bc should output the square root of 2 on your computer:Code:sqrt(2)
sqrt() is a function in bc.Code:1.4142135623
Now quit bc as follows:
Now we will execute bc again, but with two flags.Code:quit
The -q makes bc quiet, so you should get no welcome message.Code:bc -q -l
The -l starts bc with a math library, which gives you extra functions to work with. Try this again:
bc should now compute the square root of 2 to 20 decimal places:Code:sqrt(2)
When you start bc with -l it automatically sets the scale to 20.Code:1.41421356237309504880
bc also has a few new functions. Try this:
bc just calculated Euler's Number to the first power.Code:e(1)
Type this in:Code:2.71828182845904523536
bc calculates the sine of 45 in radians.Code:s(45)
To calculate cosine use c(), and to calculate arctangent use a()Code:.85090352453411842486
Now you should know how to use bc for some everyday calculations. What do we do now? If you have programming experience, programming in bc should be very easy. If you don't program, you don't need to read this section.
While loops are like this:
Conditions are just like in other programming languagesCode:while (condition) { calculations }
>, <, <=, >=, ==, !=, etc. will all work in bc
If conditions are like this:
For a programmer, this should be straightforward and easy to use.Code:if (condition) { calculations } else { calculations }
To print things in bc use the print command
prints 2 to the screenCode:print 1+1
Will print Hello, CodeCall! to the screen followed by a newline.Code:print "Hello, CodeCall!\n"
To read a number use the read() function:
Defining a function is like this:Code:number=read()
This function will take x as a number and add one to it.Code:define plusone(x) { x=x+1 return x }
If you want to run a program in bc, the command looks like this:
That's it! Feel free to post any questions, suggestions or comments. +rep is always appreciated.Code:bc program.bc
Root Beer == System Administrator's Beer
Download the new operating system programming kit! (some assembly required)
That is very cool! I always found it annoying I couldn't do math in my beloved terminal. I always assumed there was a way, but never cared enough to look into it. +rep
Thanks![]()
Root Beer == System Administrator's Beer
Download the new operating system programming kit! (some assembly required)
I never knew about this either, very cool! +rep
Behold the power of the command-line! +rep
I love Linux even more now. +rep!
EDIT: Nevermind, it won't let me do it.![]()
sudo rm -rf /
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks