- MCP server with stdio transport for local use - Search episodes, transcripts, hosts, and series - 4,511 episodes with metadata and transcripts - Data loader with in-memory JSON storage 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
179 lines
16 KiB
Plaintext
179 lines
16 KiB
Plaintext
Episode: 1202
|
|
Title: HPR1202: LiTS 025: bc
|
|
Source: https://hub.hackerpublicradio.org/ccdn.php?filename=/eps/hpr1202/hpr1202.mp3
|
|
Transcribed: 2025-10-17 21:33:05
|
|
|
|
---
|
|
|
|
Welcome to Linux in the Shell Episode 25, VC. My name is Dan Washco, I'll be your host
|
|
today, and before I get started I'd like to thank Hacker Public Radio for hosting
|
|
the website and the audio files. Remember, head on over to Hacker Public Radio for some
|
|
great content every day of the work week. Not on Saturday and Sundays, but some really great stuff
|
|
from geeky people all over the world. And if you're inclined to and have a subject in mind,
|
|
consider doing an episode of Hacker Public Radio. They're always looking for volunteer,
|
|
so all that information can be found at hackerpublicradio.org. All right, let's talk about BC,
|
|
but before we get into that, why would you want to use it? Well, let's see you want to do some
|
|
simple math, find just, you know, multiplication, addition, division, anything. And you don't want
|
|
to fire up a calculator, go find a calculator. Even though there's a ton of them handy,
|
|
that, you know, chances are they're installed on your system and your menu, but you're down on
|
|
the shell and you want to do some simple math. Well, how can you do that in a shell? You can use
|
|
the echo command, and there'd be echo, space, dollar sign, parentheses, parentheses, some statement,
|
|
parentheses, parentheses, hit enter. For instance, echo, space, dollar sign, parentheses, parentheses,
|
|
two, asteris, three, close the parentheses, both of them, and hit enter, and you're going to get six.
|
|
So you would enclose the statement within a dollar sign and two parentheses.
|
|
That's great, but it only does simple, very, very simple math, and it will not particularly
|
|
end a vision, or any other statements, work with decimals. For instance, if you were to do echo,
|
|
dollar sign, parentheses, parentheses, four divided by three, close parentheses, and you're going
|
|
to get a value of one. When the value should actually be 1.33333 to infinity, because it's repeating.
|
|
You will not get that from just using echo commands. Similarly, you can use the expression command
|
|
EXPR, and do some simple mathematics, for instance, like EXPR, space, five, space, plus, space,
|
|
five, and when you hit enter, it's going to show 10. You've got to have spaces between everything,
|
|
or else you're just going to have an echo back to you, five plus five, if you didn't put any spaces in
|
|
there. But expression can be used for simple mathematics, and again, like echo, it doesn't recognize
|
|
decimals. If you want to get a little more advanced, do a little more equations, functions, and
|
|
stuff like that, there's the BC command. Well, it's a tool, actually. BC itself is a mathematical
|
|
language. It's an arbitrary precision calculator language, is what it's called. And when you run BC,
|
|
if you just type in BC, you're going to be put into what's called interactive mode,
|
|
and interactive mode is going to show you what version it is. It's going to show you some copyright
|
|
information, and tell you that, you know, for warranty, or details, see the warranty.
|
|
It's going to put you out of a command prompt, so you're in the BC tool. That's the same thing as
|
|
echoing, or it's not echoing, but using the dash i or dash dash interactive switch to BC,
|
|
which puts you into that tool. Now, there's another switch similar to that relevant
|
|
to dash q, quiet. So when you start up BC with dash q, it doesn't show you the warranty
|
|
information to the version information. You just go right into the command, the shell, BC tool itself,
|
|
the interactive mode. Now, you can type in your statement, for instance, we just did four divided
|
|
by three, which would be four slash three, and hit enter. And you'll notice, if you do this,
|
|
you're following along, it's going to return one. You might say, well, damn, I thought you were
|
|
going to give me some of tools I could use. It's going to have decimals. Well, BC does have decimals,
|
|
and to explain why you only see one value there. You only see one as opposed to one with some decimals.
|
|
If you type in the word scale, SCALE, and hit enter, it's going to return the value of zero.
|
|
Now, what scale is? There's four special variables here that you have available to you.
|
|
An addition to scale, but scale is the number of decimal digits,
|
|
number of digits after the decimal point. So if we were to type in scale equals three,
|
|
hit enter, and do four divided by three again, you're going to see one dot three three three.
|
|
Scale, when we set scale equal to three, that means three decimal digits. So there's going to be three
|
|
up to three digits after the decimal point available. So there we have our decimals back. So
|
|
they're not gone. It's not that it doesn't recognize decimals or allow you to do decimals.
|
|
It does, but by default, the scale is set to zero. Also by default, there's not a lot of other
|
|
functions loaded into it. So there's another switch that you might want to consider using
|
|
when you start up BC, and that's the dash L. So if you want to get into more complex functions
|
|
of mathematics, if you use the dash L, it loads the standard math library when it's executed.
|
|
And then that set scale automatically to 20. So you're going to get 20 decimal digits. In
|
|
addition to that, you're also going to get sine functionality, cosine functionality. Both of those
|
|
are shown in radians. You're going to get arc tangent and arc tangent returns in radians. You're
|
|
going to get the logarithm exponential functions and bezel function sine. If you want to do sine,
|
|
it's just sine. And then the value that you want to have to get the sine of in parentheses. So
|
|
sine parenthesis X closed parenthesis. Similar with cosine, it's C parenthesis X closed parenthesis.
|
|
And then arc tangent tangent is A parenthesis X parenthesis, closed parenthesis. Then you have
|
|
logarithm, which is L, and then the X parenthesis. X-ponential function, which is E, and then X-pon
|
|
parenthesis, and the bezel, which is J, and then in parentheses, you have the number comma X,
|
|
and of X right there. Now, if you're not familiar with those values are sine, cosine,
|
|
arc tangent, logarithm, exponential functions, bezel, head on over to the website. There is some
|
|
basic information I have in the bibliography that will explain some of those values.
|
|
I don't want to really go into too much detail here, but one of the things you might not recognize
|
|
X-ponential function is not exponents, or necessarily. The X-ponential function is a function
|
|
E of X, where E is the number approximately 2.718281828, and therefore such that the function of
|
|
E of X is his own derivative. So the value of what you'll be seeing there is 2.718281828 to the power
|
|
of whatever X value you put into the power of 2, 5, 10, whatever, but it's exponential function. It's
|
|
not, if you do E of X in there, you're not going to get X to the exponent that you put in 10 of,
|
|
so you might say, well, if it's exponents, how do I enter the second value? No, no, it's an
|
|
exponential function. It's not the actual exponent exponent. To do an exponential value of a number,
|
|
like if you wanted to do 5 to the 10th power, it would be 5 carat 10 to do exponents like that.
|
|
So its exponential function is not exponents. Now, as for what bezel function is, I'm not even
|
|
going to begin to try and explain that right now. It's way beyond the scope of this show, but
|
|
that function is in there, and I have a link to it in the bottom of the big biography.
|
|
Well, let's get back to BC. So now, if you run BC with the dash L library loaded, and you were to
|
|
type in for divided by 7, which is 4 slash 7, the value return is going to be 1.3, and you're going
|
|
to see 23s after this, because it's two 20 decimal spaces by default for when the math
|
|
lib library is loaded. You can see that by typing scale, and scale will return the value of 20.
|
|
Now, I said there are three other special functions in there, special variables. I'm going to talk
|
|
about those right now. First, most simple last. Last is the value of the last number printed. So,
|
|
if we were to type last right now, we're going to get 20, because the last thing we did was we said
|
|
scale in the variable return was 20. So, if you type last, you're going to get 20. If you put
|
|
in a statement like four times eight, and you hit enter, you're going to get a value of 32 back. If
|
|
you type last, that value is now 32. Very simple. It's a handy thing that you could use to continue
|
|
on with the return of a statement or a function that you put in the BC. To other values, I base
|
|
an O base. Now, by default, BC puts you in decimal, base decimal. You can change that. You can
|
|
change the input base, which is I base, or the O base, output base, to be any other base value.
|
|
Decimal by default, binary, hex, octal, you name it. So, you would just type in
|
|
I base equals 10 for decimal. And if I were to type in 15 times 1 and hit enter, that's going to
|
|
give me 15. But if I do O base, which is output, equals 2, puts me in binary, and I type 15 times
|
|
1. The result that gets returned is going to be not in decimal anymore, but it's going to be in
|
|
binary. So, I do 15 times 1. The value is going to be 111141. That's a value of 15. If I wanted to
|
|
do hex, I would just type O base equals 8. No, I'm sorry, that's octal. And when I do that,
|
|
if I do the same thing, 15 times 1, I get a value of 117 or 17. That gives me the value of 15
|
|
back, 117 and an octal. Same thing with hex. If I do hex, O base equals 16, 15 times 1, I get
|
|
the value F returned. So, you can switch between bases with the BC command by using I base or
|
|
O base. And then if you want to set it back, it's just I base equals or O base equals 10 for decimal,
|
|
or you can exit and restart BC. The BC command respects the standard order of operations and all
|
|
mathematical functions, that basic mathematical functions. Similarly, anything that you can do in
|
|
a calculator, you can do in pretty much in the BC command. Now, where one of the things that
|
|
makes says BC apart though, is because it's a language, you can actually kind of do some programming in it
|
|
with the BC language. For instance, you can create functions. And to create a function,
|
|
the format is just like any other programming language. You type in define, we're define,
|
|
and then the function name. For instance, let's create a simple function that just converts a value
|
|
into binary, octal and hex, and then puts you back into decimal. So you would define a function,
|
|
call it Litz converter. And then in parentheses, you say the variables that you accept be passed to
|
|
that function. In our case right here, we're just going to have one variable x. And then open
|
|
parenth, curly brackets, the syntax of your function in there, all your statements for the function,
|
|
and then you close it with curly brackets, closing curly brackets. So for instance, if our function
|
|
Litz converter, we would say O base equals 2, put a semicolon, you don't have to put a semicolon,
|
|
but it's probably a good idea. I've done it both ways. Hit enter, and then print, which is a value
|
|
that would print out onto the screen, binary value is, and then double quotes, close the quotes,
|
|
semicolon, a times one, semicolon, print, and double quotes, backslash, and for a new line.
|
|
And I do, just remember that whole statement right there, print binary value equals,
|
|
and then a times n, and then another print statement. So then after that line, we do O base equals
|
|
8 to switch to octal, do the same thing, print, octal value is, and double quotes, and then
|
|
semicolon, a times one, semicolon, print, new line, and double quotes. Now we go O base equals 16,
|
|
semicolon, let me do print, and double quotes, hex value equals semicolon, put that in double
|
|
quotes, and semicolon in 8 times one, semicolon, print, new line, let me do O base equals 10,
|
|
and then print, and double quotes, all finished, and close it off, hit enter, or closing curly bracket.
|
|
Now when we run, let's converter two, you see the value of two, returned in binary,
|
|
octal, and hex, which are also going to see a value, the zero return at the end.
|
|
And you're going to say, why is the zero return? Well,
|
|
ignore the zero returns at the end of every function if return is not used. So
|
|
we're not returning anything in the function, so it returns a value of zero by default.
|
|
If we wanted to avoid the zero, we would return, and some value have the value of that be returned,
|
|
but because we're not using the return statement, it automatically returns a zero.
|
|
So that's BC in a nutshell. If you quit BC, you lose the value of your
|
|
defined function. So just be aware of that. You're not able to save it. It doesn't retain the
|
|
function, so you kind of lost there. Now, if you wanted to get some statement, return, some
|
|
mathematics done, you don't have to run BC in interactive mode. You can pass something
|
|
to BC with the echo command and pipe it to BC. So I echo in double quotes two times two
|
|
asterisk five, close to double quotes, pipe BC. That's going to pass two times five to BC,
|
|
and it's just going to spit out the value of 10. It doesn't put you in interactive mode,
|
|
but it allows you to pass or do simple mathematics, pass it to the BC command, simple statements.
|
|
Or actually any statement, you can pass to it. In fact, if you want to pass a whole
|
|
string of statements to BC, like with the echo command, just separate each statement with a
|
|
semicolon, like echo, double quotes, O base equals two, semicolon five times five, semicolon,
|
|
O base equals 10, semicolon last. What that's going to do is it's going to pass all that to BC,
|
|
so BC is going to process it one thing at a time. It's going to set the output base to binary.
|
|
It's going to process five times five, so you're going to get 25. It's going to show you the value
|
|
of 25 in binary. Then it's going to set the O base back output base back to decimal, but equals 10.
|
|
And then with the last command, it's going to show you the value of the last number that's put out,
|
|
because now it's in decimal, it's going to show you what it would be in decimal 25. So there we go.
|
|
That is a way BC command is a way to do mathematical function statements from the command line,
|
|
very easy or very complex, however deep you want to get. It's a great handy tool if you're
|
|
down there and you need to figure out some kind of value. A good example is I wanted to find something
|
|
in, convert something from megabytes to bytes. Instead of having to whip out a calculator,
|
|
I could just type in the BC command, the value of that multiply it by 1024 multiply again by 1024
|
|
to go from megabytes down to kilobytes down to bytes and get that number there. So I can use it
|
|
in the split command for whatever I wanted to, because this version of split I was using at the time,
|
|
only except in input values of bytes, not in kilobytes or megabytes that I wanted to,
|
|
it was all the version of split. Anyway, that is BC in a nutshell. You might be saying, Dan,
|
|
is there an easier way to get that value right there, convert something from one format to another?
|
|
I'm going to leave you with that because there is and that is foreshadowing my friend of an
|
|
upcoming show. This has been Linux in the show episode 25 talking about BC and some mathematics
|
|
on the command line. I hope that you will join me next week and two weeks, I'm going to
|
|
fortnight to continue on some other really handy mathematical tools on the command line. Thanks a
|
|
lot and have a great day.
|
|
You have been listening to Hacker Public Radio at Hacker Public Radio. We are a community podcast
|
|
network that releases shows every weekday Monday through Friday. Today's show, like all our shows,
|
|
was contributed by a HPR listener like yourself. If you ever consider recording a podcast,
|
|
then visit our website to find out how easy it really is. Hacker Public Radio was founded by
|
|
the Digital Dark Pound and Neil Phonomicum Computer Club. HPR is funded by the binary
|
|
revolution at binref.com. All binref projects are crowd-responsive by linear pages.
|
|
From shared hosting to custom private clouds, go to lunarpages.com for all your hosting needs.
|
|
Unless otherwise stasis, today's show is released under creative commons, attribution, share
|
|
in life, digitalizes.
|