As a Linux administrator you might use the command line calculator many times in a day for doing some math, using bc command.
I used the bc command several times while creating LVM to calculate PE values.
The command line calculator will allow us to perform all kinds of operations such as scientific, financial, or even simple calculation. Also, it can be used in shell scripts for complex math.
This article describes best five commands to perform your calculations using the terminal. They are listed below:
- bc : Basic calculatoer used for basic math.
- calc : It’s alternative to bc.
- gcalccmd : is the console version of Gnome Calculator utility.
- qalc : is the console version of the calculator app of Qalculate.
- expr & echo : Linux command is used for very basic math calculation.
1) How to perform calculation in Linux using bc command?
bs stands for ‘Basic Calculator’. It offers you everything you would expect from a scientific, financial, or even simple calculator.
bc is a language that supports arbitrary precision numbers with interactive execution of statements. There are some similarities in the syntax to the C programming language.
By default bc command is pre-installed in all the Linux systems. If not, use the following commands to install it depending on your environment:
$ sudo dnf install bc [Fedora & CentOS/RHEL 8] $ sudo apt install bc [Ubuntu/Debian] $ sudo pacman -S bc [Arch Linux/Manjaro] $ sudo yum install bc [CentOS/RHEL 6/7] $ sudo zypper install bc [openSUSE Leap]
1.a) How to use bc command
"bc" on your terminal to launch the bc command and use the following symbols for calculation:
- Plus : Addition
- Minus : Subtraction
- Forward Slash : Division
- Asterisk: Used for Multiplication
'quit' to exit the bc once you’re done.
$ bc bc 1.07.1 Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006, 2008, 2012-2017 Free Software Foundation, Inc. This is free software with ABSOLUTELY NO WARRANTY. For details type `warranty'. 1+2 3 10-5 5 2*5 10 10/2 5 (2+4)*5-5 25 quit
-l” flag to define the standard math library:
$ bc -l bc 1.07.1 Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006, 2008, 2012-2017 Free Software Foundation, Inc. This is free software with ABSOLUTELY NO WARRANTY. For details type `warranty'. 3/5 .60000000000000000000 quit
2) How to use the Linux terminal as a Calculator with calc command?
calc is an arbitrary precision calculator that comes with a rich set of built-in mathematical and programmatic functions.
It’s a simple calculator that allow us to perform all kinds of calculation in Linux command line, which is not pre-installed and you need to use the below commands to install it.
For “Fedora” & “RHEL/CentOS 8” systems, use dnf command to install calc :
$ sudo dnf install calc
For “Debian/Ubuntu” based systems, use apt command to install calc :
$ sudo apt install apcalc
For “Arch Linux” based systems, use pacman command to install calc :
$ sudo pacman -S calc
For “RHEL/CentOS 6/7” systems, use yum command to install calc :
$ sudo yum install calc
For “openSUSE Leap” system, use zypper command to install calc :
$ sudo zypper install calc
2.a) Calculations with calc command
It’s an alternative to bc and can be used for any kind of mathematical calculation right from the terminal, as shown below.
$ calc C-style arbitrary precision calculator (version 220.127.116.11) Calc is open software. For license details type: help copyright [Type "exit" to exit, or "help" for help.] ; 5+1 6 ; 5-1 4 ; 5*2 10 ; 10/2 5 ; quit
$ calc 3/5 0.6
3) How to perform calculation in Linux using gcalccmd command?
gnome-calculator is the official calculator of the GNOME desktop environment, whereas ‘gcalccmd’ is the console version of Gnome Calculator utility. By default it has installed in the GNOME desktop, no matter your distro.
3.a) gcalccmd command to perform calculation
I have added few examples on this, and this can do calculations similar to other command line calculators mentioned above.
Note: To exit from the gcalccmd command, press “Ctrl+d” or enter “quit”
$ gcalccmd > 5+1 6 > 5-1 4 > 5*2 10 > 10/2 5 > sqrt(16) 4 > 3/5 0.6 > quit
4) How to perform calculation in Linux using qalc command?
Qalc is the console version of the calculator app of Qalculate, which is used as multi-purpose calculator. It is simple to use but provides versatile features.
If you are looking for a calculator with some extra features like currency conversion, percentage calculation and unit calculations, Qalc will meet your needs.
$ sudo dnf install libqalculate [Fedora & CentOS/RHEL 8] $ sudo apt install libqalculate [Ubuntu/Debian] $ sudo pacman -S libqalculate [Arch Linux/Manjaro] $ sudo yum install libqalculate [CentOS/RHEL 6/7] $ sudo zypper install libqalculate [openSUSE Leap]
4.a) qalc command to perform calculation
I have added few examples below to demo this:
$ qalc > 5+1 5 + 1 = 6 > ans*2 ans * 2 = 12 > ans-2 ans - 2 = 10 > 1 USD to INR 1 * dollar = approx. INR 73.06961 > 1 SGD to INR 1 * SGD = approx. INR 54.491817 > quit
5) How to perform basic calculation in Linux using expr & echo command?
If you just want to perform basic calculations, then you don’t need to install any calculator application on your system. You can do them in bash shell using expr & echo commands.
Both are included in the “coreutils” package, which is pre-installed almost on all Linux distributions.
5.a) expr command to perform calculation
Use the following format for basic calculations.
For addition, subtraction & division
$ expr 5 + 1 6 $ expr 5 - 1 4 $ expr 10 / 2 5
For multiplication: The ‘backslash’ should be added before the asterisk :
$ expr 10 \* 2 20
5.b) echo command to perform calculation
$ echo $((5+5)) 10 $ echo $((10-4)) 6 $ echo $((10*4)) 40 $ echo $((10/2)) 5
We’ve shown you how to use the Linux terminal as a calculator, regardless of your distro.
If you found this article helpful, please do share with your friends and spread the knowledge. Please feel free to comment below if you have any queries/concerns. We will get back to you as soon as we can. Happy learning!