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

Element Timeline implementation

Table of contents Components - Timeline Custom no...

In-depth analysis of MySQL indexes

Preface We know that index selection is the work ...

Explanation of Dockerfile instructions and basic structure

Using Dockerfile allows users to create custom im...

Summary of various forms of applying CSS styles in web pages

1. Inline style, placed in <body></body&g...

Explanation of nginx load balancing and reverse proxy

Table of contents Load Balancing Load balancing c...

Vue+ssh framework to realize online chat

This article shares the specific code of Vue+ssh ...

Vite2.0 Pitfalls

Table of contents Vite project build optimization...

HTML table markup tutorial (16): title horizontal alignment attribute ALIGN

By default, the table title is horizontally cente...

Why does your height:100% not work?

Why doesn't your height:100% work? This knowl...

How to use the VS2022 remote debugging tool

Sometimes you need to debug remotely in a server ...

Problems and solutions encountered when connecting node to mysql database

I installed a new version of MySQL (8.0.21) today...

Some tips on deep optimization to improve website access speed

<br />The website access speed can directly ...

MySQL data compression performance comparison details

Table of contents 1. Test environment 1.1 Hardwar...

How to configure static network connection in Linux

Configuring network connectivity for Linux system...