Detailed explanation of 5 commands using calculator in Linux command line

  • 2021-08-21 22:05:07
  • OfStack

Hello, everyone, I'm Liang Xu.

When using Linux, we sometimes need to do some calculations, so we may need to use a calculator. On the Linux command line, there are many calculator tools that allow us to perform scientific calculations, financial calculations, or simple calculations. Of course, we can also use these commands to perform more complex mathematical operations in Shell scripts.

Here we mainly introduce five kinds of command line calculators:

bc calc expr gcalccmd qalc

1. How to perform calculations using bc in Linux

bc C is a language that supports arbitrary precision numbers in the form of interactive statement execution. Its syntax is similar to that of C.

There is a standard math library available in the command line options of bc. If relevant, bc will define the standard math library before processing any files, and bc will process the codes of each file listed in the command in the order of the command.

By default, bc is installed in all Linux distributions. If you don't have bc on your system, you can install it yourself by following the following command:

For Fedora systems, use the DNF command for installation

$ sudo dnf install bc

For Debian/Ubantu systems, install using the APT-GET or APT command

$ sudo apt install bc

For systems based on Arch Linux, use the Pacman command for installation

$ sudo pacman -S bc

For RHEL/CentOS systems, use the YUM command for installation

$ sudo yum install bc

For the openSUSE Leap system, use the Zypper command for installation

$ sudo zypper install bc

How to use the bc command

We can use the bc command to perform +-*/^% and other calculations directly in 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

13/5
2

(2+4)*5-5
25

2^3
8

8%3
2

quit

Use -l To define the standard math library. By default, 3/5 is 0 in the result of bc, because it is only rounded. If you want to get the correct answer, you need to use -l Option.


$ 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 calc for calculation in Linux

calc Is an arbitrary precision calculator, is a simple calculator that can perform various calculations on the command line.

If you need to install the calc command, you can refer to the above bc command installation method.

How to use the calc command

We can use calc to interactively perform various types of calculations in the terminal.


$ 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
; 13/5
  2.6
; 13%5
  3
; 2^4
  16
; 9^0.5
  3	
; quit

Of course, you can also use it in non-interactive mode:

$ calc 3/5 0.6

3. How to use expr for calculation in Linux

expr is part 1 of the core tool library, so we don't need to install it.

We can use the following command for basic calculation:


$ expr 2 + 3
6
$ expr 6 - 2
4
$ expr 3 * 4
12
$ expr 15 / 3
5

But 1 Be careful that numbers and symbols need to be separated by spaces, otherwise the command will not recognize the expression you wrote:


$ expr 2+3
2+3
$ expr 2*3
2*3

4. How to use gcalccmd for calculation in Linux

gnome-calculator Is the official calculator for the GNOME desktop environment. gcalccmd is the console version of the Gnome Calculator tool. By default, gcalccmd is installed on the GNOME desktop.

Calculate using the gcalccmd command

gcalccmd also has an interactive test interface, and the calculation formula is no different from other calculators.


$ gcalccmd
> 5+1
6

> 5-1
4

> 5*2
10

> 10/2
5

> sqrt(16) 
4

> 3/5
0.6

> quit

5. How to use qalc for calculation in Linux

calc0 Is a multi-functional cross-platform desktop calculator. It is relatively simple to use, but its function is 10 points powerful. It provides a powerful multifunctional math library, as well as a number of practical tools to meet daily needs (such as currency conversion and percentage calculation, etc.).

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

For Fedora systems, use the DNF command for installation

$ sudo dnf install libqalculate

For Debian/Ubantu systems, install using the APT-GET or APT command

$ sudo apt install libqalculate

For systems based on Arch Linux, install using the Pacman command

$ sudo pacman -S libqalculate

For RHEL/CentOS systems, install using the YUM command

$ sudo yum install libqalculate

For the openSUSE Leap system, install using the Zypper command

$ sudo zypper install libqalculate

Calculate using the qalc command


$ 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

6. How to calculate using the Shell command

In fact, under the command line of Shell, we can directly use echo, awk and other commands to calculate directly, which is convenient for 10 points.


$ echo $((5+5))
10
$ cat data | awk '{sum+=$1} END {print "Sum = ", sum}' #  Calculation data The sum of the data in the file 

Summarize


Related articles: