5 Commands to Use the Calculator in Linux Command Line

5 Commands to Use the Calculator in Linux Command Line

Hello everyone, I am Liang Xu.

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

Here we mainly introduce 5 command line calculators:

  • bc
  • calc
  • expr
  • gcalccmd
  • qalc

1. How to perform calculations using bc in Linux

bc is a language that supports arbitrary precision numbers in an interactive statement execution mode. Its syntax has some similarities with C language.

bc has a standard math library available in the command line options. If required, bc will define the standard math library before processing any files. bc will process the code of each file listed in the command in the order in which it is listed.

By default, bc is installed in all Linux distributions. If bc is not available in your system, you can install it yourself by following the command below:

For Fedora system, use DNF command to install

$ sudo dnf install bc

For Debian/Ubantu systems, use APT-GET or APT command to install

$ sudo apt install bc

For Arch Linux based systems, use Pacman command to install

$ sudo pacman -S bc

For RHEL/CentOS systems, use the YUM command to install

$ sudo yum install bc

For openSUSE Leap system, use Zypper command to install

$ sudo zypper install bc

How to use bc command

We can use the bc command to perform various calculations such as + - * / ^ % 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 just rounded. If you want to get the correct answer, you need to use the -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
.6000000000000000000

quit

2. How to use calc in Linux

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

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

How to use the calc command

We can use calc to perform various types of calculations interactively 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 calculations in Linux

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

We can use the following commands to perform basic calculations:

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

But be sure to note that the numbers and symbols need to be separated by spaces, otherwise the command will not recognize the expression you write:

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

4. How to use gcalccmd to perform calculations 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 in the GNOME desktop.

Use the gcalccmd command to calculate

gcalccmd also has an interactive interface, and the calculation formulas are written in the same way as 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 calculations in Linux

Qalculate is a multi-functional cross-platform desktop calculator. It is relatively simple to use, but its functions are very powerful. It provides some very powerful and versatile math libraries, as well as some practical tools for everyday needs (such as currency conversion and percentage calculation).

Qalculate's features include a large customizable function library, unit calculations and conversions, 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

$ sudo dnf install libqalculate

For Debian/Ubantu systems, use APT-GET or APT command to install

$ sudo apt install libqalculate

For Arch Linux based systems, use Pacman command to install

$ sudo pacman -S libqalculate

For RHEL/CentOS systems, use the YUM command to install

$ sudo yum install libqalculate

For openSUSE Leap system, use Zypper command to install

$ sudo zypper install libqalculate

Use the qalc command to calculate

$ 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?

 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 use Shell commands for calculations

In fact, under the Shell command line, we can directly use commands such as echo and awk to perform calculations, which is very convenient.

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

Summarize

This concludes this article about 5 calculator commands in Linux command line. For more information about calculator commands in Linux command line, please search previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Use of Linux gzip command
  • Usage of Linux userdel command
  • Use of Linux date command
  • Use of Linux telnet command
  • Linux gzip command compression file implementation principle and code examples
  • Use of Linux ls command
  • Use of Linux ifconfig command

<<:  Reasons why MySQL 8.0 statistics are inaccurate

>>:  Antd+vue realizes the idea of ​​dynamic verification of circular attribute form

Recommend

A brief introduction to the general process of web front-end web development

I see many novice students doing front-end develop...

Detailed explanation of how to use join to optimize SQL in MySQL

0. Prepare relevant tables for the following test...

How to solve the timeout during pip operation in Linux

How to solve the timeout problem when pip is used...

Rendering Function & JSX Details

Table of contents 1. Basics 2. Nodes, trees, and ...

How to use CSS style to vertically center the font in the table

The method of using CSS style to vertically cente...

Detailed explanation of MySQL partition table

Preface: Partitioning is a table design pattern. ...

A simple tutorial on how to use the mysql log system

Table of contents Preface 1. Error log 2. Binary ...

A brief analysis of SQL examples for finding uncommitted transactions in MySQL

A long time ago, I summarized a blog post titled ...

Use the CSS border-radius property to set the arc

Phenomenon: Change the div into a circle, ellipse...

How to prevent event bubbling in JavaScript

What we need to pay attention to is that the char...

A brief discussion on docker-compose network settings

Networks usage tutorial Official website docker-c...

Summary of MySQL time statistics methods

When doing database statistics, you often need to...

View the frequently used SQL statements in MySQL (detailed explanation)

#mysql -uroot -p Enter password mysql> show fu...