BreakingExpress

Do math within the Linux shell with GNU bc

Most POSIX techniques include GNU bc, an arbitrary precision numeric processing language. Its syntax is just like C, but it surely additionally helps interactive execution of statements and processing knowledge from normal in (stdin). For that motive, it is typically the reply to the query, “How do I do math in the Linux shell?” This model of response is frequent on-line:



While that is completely legitimate, few customers argue it is elegant in comparison with one thing extra intuitive, corresponding to:

$ 1+1  #this doesn't work
2

The interactive mode is just a little simpler:



But interactive modes do not at all times match the specified and intuitive workflow of one thing easy, like simply typing within the calculation you need. For this, I counsel Bluebat’s calculator in pure Bash.

What bc really supplies is a mathematical language for superior calculation.

Advanced features with mathlib

On its personal, bc supplies fundamental math features. You can take a look at them within the interactive mode:



Use the --mathlib possibility to achieve superior features, together with sine, cosine, tangent, and extra. In the interactive mode, you may take a look at a few of them. Here’s the cosine of 90:

c(90)
-.44807361612917015236

The sine of 9:

s(9)
.41211848524175656975

Creating your personal bc features

You may also create your personal features in bc. Function definitions begin with the outline key phrase and are enclosed with braces. Here is a straightforward perform entered into an interactive session that returns no matter quantity it is given:

$ bc
outline echo(n)

In the identical interactive session, try it out:



If statements in bc

The bc language additionally has a wide range of management statements, the best of which is that if/else. The syntax could seem acquainted at first look, however there are subtleties in how braces are dealt with. Note that the else clause of an if-statement is enclosed in braces, whereas the then clause isn’t, however each are terminated with a semicolon. Here’s a perform to seek out absolutely the worth of a quantity n:

outline abso(n)

In the identical interactive session, try it out:



Importing knowledge into bc

Working in an interactive session is tolerable for fast calculations and experimentation, however you lose your knowledge if you stop, and it is troublesome to edit if you make errors. Fortunately, bc can load variables and features from exterior information.

Here’s a file containing two variables (sol and foo) and a customized abso perform to seek out an absolute worth:

sol=299792458

foo=42

outline abso(n)

Save this right into a file known as bcvars.bc, so you may import it right into a bc interactive session:

$ bc bcvars.bc
foo
42
sol
299792458
abso(-23)
23

Power-up your math with bc

The bc language is comparatively easy, offered you understand sufficient math to assemble the equation for no matter you are attempting to perform. While bc by default supplies helpful fundamental features and permits you to create your personal, you may scale back your workload by standing on the shoulders of giants. Files loaded with new features, each for mathematic fundamentals in addition to for particular duties (as an illustration, calculating compound curiosity), can be found from GNU bc page, and full documentation for bc is obtainable.

If you are considering doing higher math in a shell, strive bc. It will not flip you right into a arithmetic genius, but it surely simply would possibly make it simpler to grow to be one.

Exit mobile version