Skip to content

Creating a quick calculation function on Linux

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 ; }

Troubleshooting your bash scripts in Linux
 

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.

To read this article in full, please click here

Source:: Network World – Data Center