Hello everyone, this is Liang Xu.
When using Linux, we sometimes need to do some calculations, so 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 kinds of command line calculators:
bc
calc
expr
gcalccmd
qalc
1. How to perform calculations using bc in Linux
bc
It is a language that supports arbitrary-precision numbers in interactive statement execution. Its syntax is similar to that of C language.
There is a standard math library available in the command line options of bc. If there are relevant requirements, 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 of the command. .
By default, bc is installed in all Linux distributions. If you don't have bc in your system, you can install it yourself according to the following command:
For Fedora system, use DNF command to install
$ sudo dnf install bc
For Debian/Ubantu system, use APT-GET or APT command to install
$ sudo apt install bc
For Arch Linux-based systems, use the 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 the bc command
We can use the bc command to directly perform various calculations such as +-* / ^% 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 right answer, then you need to use the -l
option of.
$ 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
It is an arbitrary precision calculator and 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 bc command installation method above.
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 of the core tool library, so we don't need to install it.
We can use the following commands for basic calculations:
$ expr 2 + 3
6
$ expr 6 - 2
4
$ expr 3 * 4
12
$ expr 15 / 3
5
But it must be noted that 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 for calculation in Linux
gnome-calculator
It is the official calculator of 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 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
Qalculate
It is a multifunctional cross-platform desktop calculator. It is relatively simple to use, but the function is very powerful. It provides some very powerful multi-functional math libraries, and some practical tools to meet daily needs (such as currency conversion and percentage calculation, etc.).
The functions of Qalculate include a large number of customizable function libraries, unit calculations and conversions, symbolic calculations (including integrals and equations), arbitrary precision, uncertainty propagation, interval arithmetic, drawing, and user-friendly interfaces (GTK + and CLI).
For Fedora system, use DNF command to install
$ sudo dnf install libqalculate
For Debian/Ubantu system, use APT-GET or APT command to install
$ sudo apt install libqalculate
For Arch Linux-based systems, use the 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? 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 use Shell commands to calculate
In fact, under the Shell command line, we can directly use echo, awk and other commands to directly perform calculations, which is very convenient.
$ echo $((5+5))
10
$ cat data | awk '{sum+=$1} END {print "Sum = ", sum}' # 计算data文件里数据之和
Finally, recently many friends asked me for the Linux learning roadmap , so based on my experience, I spent a month staying up late in my spare time and compiled an e-book. Whether you are in an interview or self-improvement, I believe it will help you! The directory is as follows:
Give it to everyone for free, just ask you to give me a thumbs up!
Ebook | Linux development learning roadmap
I also hope that some friends can join me to make this e-book more perfect!
Gain? I hope the old irons will have a three-strike combo so that more people can read this article
Recommended reading: