Home / How-to Guides / Command line Calculator on Unix, Linux and Mac

Command line Calculator on Unix, Linux and Mac

I mostly use terminal windows to do most of my things. Even when I need to do some calculations or arithmetic operations, I prefer doing it on the terminal command line. When everything you could do using the terminal, why should you use a GUI calculator? One of the most valuable features of using this is that it helps you solve complex calculations or arithmetic operations on the command line.

Command line Calculator on Unix, Linux and Mac_thumb[4]

It becomes challenging to solve some complex arithmetic operations using the GUI calculator. Adding brackets and braces for the equation in GUI Calculator is not easy, and if you add any incorrect bracket, you end up with the wrong result. Calculating on the command line will display all the expressions on a single line, making you see what you have typed and preventing you from the typo mistakes.

This function is created with the help of the bash shell. Bash Shell comes by default in Linux, Unix, and Mac OS. It can also work with Microsoft’s Windows using Cygwin. Check my previous article on “Calculate using DOS Command in windows” To create a Calculator, enter the below function on the command line. Add this function command to ~/.bashrc, so that you don’t need to retype this function again after restarting the terminal.

calc(){ awk "BEGIN{ print $* }" ;}

Command line Calculator Linux

Now type “calc” on the command line followed by the calculation you want to perform. If you are using space or a special character, make sure you add quotes to your calculation or expression; otherwise, you’ll get an error. A simple example of an arithmetic operation, 10+20-20*10/10 gives the output as 10. You can use parenthesis or brackets for complex expressions, but don’t forget to put quotes to your expression, or you will end up with the error, check the image beside. Below are some examples of such an arithmetic operation using the terminal command line.

calc 10*(20/10) answer is 20<

calc “(20*10)/2+100” answer is 200

calc “((4+(2*3))/ 9)- 45.20” answer is –44.08888

This function to calculate on the command line will also work on Windows using Cygwin, but I have never tested this using Cygwin. Did the above trick was helpful and informative to you? 

If you find this article interesting, then please make a little more effort by sharing and spreading the knowledge to others.

About Roshan Karkera

Roshan Karkera a.k.a. k.roshan. Blogger by passion and software engineer by profession. He believes in sharing knowledge, which made him to start his own blog. He is very much keen & passionate about new technology & Science. If you'd like to connect with him, follow at Google plus or Twitter.

2 comments

  1. That will help me, i generally work on terminal

  2. that is working for me, at least its not ignoring decimal value and its able to calculate difficult expression too.

Leave a Reply

Your email address will not be published.