Five commands to use Calculator from Linux command line

As a Linux administrator you may use the command line calculator many times in a day for some purpose.

I had used this especially when LVM creation using the PE values.

There are many commands available for this purpose and I’m going to list most used commands in this article.

These command line calculators will allow us to perform all kind of actions such as scientific, financial, or even simple calculation.

Also, we can use these commands in shell scripts for complex math.

In this article, I’m listing the top five command line calculator commands.

  • bc:      An arbitrary precision calculator language
  • calc: arbitrary precision calculator
  • expr: evaluate expressions
  • gcalccmd: gnome-calculator – a desktop calculator
  • qalc:
  • Linux Shell:

How to perform calculation in Linux using bc Command?

bs stands for Basic 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.

A standard math library is available by command line option. If requested, the math library is defined before processing any files. bc starts by processing code from all the files listed on the command line in the order listed.

After all files have been processed, bc reads from the standard input. All code is executed as it is read.

By default bc command has installed in all the Linux system. If not, use the following procedure to install it.

For Fedora system, use DNF Command to install bc.

$ sudo dnf install bc

For Debian/Ubuntu systems, use APT-GET Command or APT Command to install bc.

$ sudo apt install bc

For Arch Linux based systems, use Pacman Command to install bc.

$ sudo pacman -S bc

For RHEL/CentOS systems, use YUM Command to install bc.

$ sudo yum install bc

For openSUSE Leap system, use Zypper Command to install bc.

$ sudo zypper install bc

How to use the bc command:

We can use the bc command to perform all kind of calculation right from the terminal.

$ 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

Use -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

How to perform calculation in Linux using calc command?

calc is an arbitrary precision calculator. It’s a simple calculator that allow us to perform all kind of calculation in Linux command line.

For Fedora system, use DNF Command to install calc.

$ sudo dnf install calc

For Debian/Ubuntu systems, use APT-GET Command or APT Command to install calc.

$ sudo apt install calc

For Arch Linux based systems, use Pacman Command to install calc.

$ sudo pacman -S calc

For RHEL/CentOS 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

How to use the calc Command:

We can use the calc command to perform all kind of calculation right from the terminal.

Interactive mode

$ calc
C-style arbitrary precision calculator (version 2.12.7.1)
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

Non-Interactive mode

$ calc 3/5
	0.6

How to perform calculation in Linux using expr command?

Print the value of EXPRESSION to standard output. A blank line below separates increasing precedence groups. It’s part of core utils so, we no need to install it.

expr command to perform calculation :

Use the following format for basic calculations.

For addition

$ expr 5 + 1
6

For subtraction

$ expr 5 - 1
4

For division.

$ expr 10 / 2
5

How to perform calculation in Linux using gcalccmd command?

gnome-calculator is the official calculator of the GNOME desktop environment. gcalccmd is the console version of Gnome Calculator utility. By default it has installed in the GNOME desktop.

gcalccmd Command to perform calculation

I have added few examples on this.

$ gcalccmd

> 5+1
6

> 5-1
4

> 5*2
10

> 10/2
5

> sqrt(16)   
4

> 3/5
0.6

> quit

How to perform calculation in Linux using qalc command?

Qalculate is a multi-purpose cross-platform desktop calculator. It is simple to use but provides power and versatility normally reserved for complicated math packages, as well as useful tools for everyday needs (such as currency conversion and percent calculation).

Features include a large library of customizable  functions, unit calculations and conversion, symbolic calculations (including integrals and equations), arbitrary precision, uncertainty propagation, interval arithmetic, plotting, and a user-friendly interface (GTK+ and CLI).

For Fedora system, use DNF Command to install qalc.

$ sudo dnf install libqalculate

For Debian/Ubuntu systems, use APT-GET Command or APT Command to install qalc.

$ sudo apt install libqalculate

For Arch Linux based systems, use Pacman Command to install qalc.

$ sudo pacman -S libqalculate

For RHEL/CentOS systems, use YUM Command to install qalc.

$ sudo yum install libqalculate

For openSUSE Leap system, use Zypper Command to install qalc.

$ sudo zypper install libqalculate

qalc command to perform Calculation

I have added few examples on this.

$ qalc
> 5+1

  5 + 1 = 6

> ans*2

  ans * 2 = 12

> ans-2

  ans - 2 = 10

> 1 USD to INR
It has been 36 day(s) since the exchange rates last were updated.
Do you wish to update the exchange rates now? y

  error: Failed to download exchange rates from coinbase.com: Resolving timed out after 15000 milliseconds.
  1 * dollar = approx. INR 69.638581

> 10 USD to INR

  10 * dollar = approx. INR 696.38581

> quit

How to perform calculation in Linux using Linux Shell command?

We can use the shell commands such as echo, awk, etc to perform the calculation.

Addition using echo command.

$ echo $((5+5))
10

Magesh Maruthamuthu

Love to play with all Linux distribution

You may also like...