Creating a quick calculation function on Linux

With the help of bash, you can streamline applying the same function to different mathematical values.

One hand uses a calculator while the other holds banknotes.
wutzkohphoto / Shutterstock

Anytime you're planning to do a lot of calculations on a Linux system, you can use the power of bash to create a quick function and then use it repeatedly to do the calculations for you. In this post, we'll look at how this trick works and what you need to be aware of to ensure that your calculations are correct.

Let's start with this mathematical function as an example:

$ ? () { echo "$*" | bc ; }

 

This command sets up a function that will pass the values and mathematical operators that you provide as arguments to the bc calculator command. Note that to call the function, you simply type a "?" followed by the arguments. In the first example below, the arguments are 1, followed by the multiplication character "*", followed by a 2, a "+" sign and a 3. The result is 5.

$ ? 1*2+3
5

Once a quick function like that shown above is set up, you can run a long series of calculations using just the "?" followed by the arguments without having to perform each calculation with commands like these:

$ echo 19*2+5 | bc
43
$ echo 2+5*11 | bc
57

Instead, you focus on just the calculations.

$ ? 19*2+5
43
$ ? 2+5*11
57

Understand that a function defined in this way will no longer be available once you log out unless you add it to your .bashrc file as the bottom line of this .bashrc file shows:

$ tail -1 .bashrc
? () { echo "$*" | bc ; }

It's important to understand that, for bc, the multiplication or division portion of a calculation takes precedence over any addition and subtraction. In the first example below, 19*2 is computed before 5 is added. In the second example, 5*19 is computed before the 2 is added.

$ ? 19*2+5
43
$ ? 2+5*19
97

If you want to override the normal precedence of multiplication or division over addition or subtraction, use a command lke this one with the addition portion of the equation enclosed in parentheses:

$ ? '(2+5)*19'
133

The "2+5" (i.e., 7) is then calculated before the result is multiplied by 19.

Note that the expression in the above example must also be enclosed in single quotes.

The bc command is, of course, not limited to addition and multiplication. In this next command example, we are calculating the square of 11.

$ ? 11^2
121

We can also square negative numbers. As shown below, the square of -2 is 4.

$ ? -2^2
4

You can also calculate using higher powers. For example, the first example below calculates the cube of -2 which is -8, and the second calculates the eighth power of -2 which is 256.

$ ? -2^3
-8
$ ? -2^8
256

In the examples below, we first divide 121 by 2. While 60 isn't quite correct, we can show the remainder using the % operator.

$ ? 121/2
60
$ ? 121%2
1

If you want a more accurate answer, you can also specify a scale. This tells bc how many decimal places to display in the result.

$ ? 'scale=2;121/2'
60.50

Other uses of the quick function

This section explains how you can set up a quick function to work with the bc command and shows the bc command's operators. However, the quick function setup is not limited to use with bc. If you wanted to be reminded repeatedly about what time it is, you could use a function like this one:

$ ? () { echo -n "It's already "; date; echo "Work faster!"; }

At this point, you could simply type "?" any time you want to be nagged to work faster. Nothing more would be required because this particular quick function doesn't require any arguments.

$ ?
It's already Mon Apr 18 04:33:43 PM EDT 2022
Work faster!
$ ?
It's already Mon Apr 18 04:33:51 PM EDT 2022
Work faster!

Maybe you can come up with more useful commands that you'd like to run with very little effort. In fact, the "+" and "@" signs seem to work as well as the "?" for setting up your quick functions, so you could have several functions set up at the same time.

Join the Network World communities on Facebook and LinkedIn to comment on topics that are top of mind.
Related:

Copyright © 2022 IDG Communications, Inc.