1. Introduction

In Linux, sometimes we need to perform mathematical tasks and operations directly from the command-line interface (CLI).

In this tutorial, we’ll delve into several command-line calculators, each with its own set of capabilities, to cater to our specific mathematical needs.

2. Using bc

The basic calculator (bc) is one of the most popular command-line calculators in Linux. It’s compliant with POSIX standards and required to build the Linux kernel.

In short, bc is a feature-rich arbitrary precision calculator that can handle complex mathematical operations.

2.1. Basic Calculation

Let’s launch bc and perform basic arithmetic operations:

$ 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'.
x=7
y=3
z=5
(x+y)*z
50

Here, we define three variables: x, y, and z. Then, we input the expression (x+y)*z. After that, bc outputs 50 as the result from this computation.

2.2. Shell Operation

Of course, bc also accepts arguments, so we can call it directly in the shell:

$ bc <<< "3 * 5"
15

In this instance, we use the Bash <<< here-string feature to pass the string “3 * 5” as input to the bc calculator. Then, bc computes the result of the multiplication operation, and 15 is shown in the output immediately below the command.

2.3. Using Functions

We can also define our own functions for more complex calculations:

define circle_area(radius) {
  return 3.14159 * radius * radius;
}

circle_area(5)

78.53975

In this case, we define a function named circle_area() that takes one parameter, the radius of a circle. Then, we call the function with 5 as the argument. Finally, we see the returned area of a circle which is 78.53975.

3. Using calc

calc is a straightforward command-line calculator that provides a basic means of performing arithmetic calculations.

Its pros lie in its simplicity and ease of use, making it a quick solution for basic math operations. However, it lacks advanced mathematical functions, interactive capabilities, and constants commonly needed in calculations.

First, let’s install calc via apt-get and sudo:

$ sudo apt-get install calc

Once installed, we use calc to calculate:

$ calc
C-style arbitrary precision calculator (version 2.12.7.2)
Calc is open software. For license details type: help copyright
[Type "exit" to exit, or "help" for help.]

; (9*7)+(6/2)
	66

In this instance, the input expression is (9*7)+(6/2) and the output is the result 66. The calc calculator uses ; as a prompt.

Moreover, the command accepts direct arguments:

$ calc 33 * 22
	726

The calc calculator evaluates the expression 33 *22, performs the multiplication, and displays the result, which is 726.

Of course, we can also define variables:

; x=2
; y=3
; (x^2)+(y^3)
	31

In this case, we define x and y, calc evaluates the expression in the third line and prints the result, which is 31.

4. Using qalc

qalc is a command-line calculator in Linux that provides an interactive mode to enter expressions in a natural, human-readable format. Hence, we can use it to perform various mathematical operations and conversions, including trigonometric functions, logarithms, and more.

First, let’s install qalc:

$ sudo apt-get install qalc

Once installed, we can use qalc to access a wide range of mathematical functions and conversions:

$ qalc
> sin(30 degrees)
  sin(30 × degree) = 1/2 = 0.5

> rand(10)
  rand(10) = 5

> 120 usd to euro
  120 × USD ≈ €106.5814015

> now
  now ≈ "2023-10-23T11:53:53"

> tomorrow - yesterday
  tomorrow − yesterday = 2 d

> 55 to bin
  55 = 0011 0111

As we can see, qalc is more like a shell than other calculators, as it’s more versatile than a basic calculator. The qalc calculator uses > as its prompt.

5. Using octave

octave is a numerical computing environment commonly used in Linux for performing mathematical and scientific calculations.

In some respects, it’s similar to MATLAB and provides a powerful command-line interface for numerical analysis, data manipulation, and visualization.

First, let’s install octave:

$ sudo apt-get install octave

Then, we can perform a numerical computing:

$ octave

...
octave:1> % Set the number of Fibonacci numbers to generate
octave:1> n = 10;
octave:2> % Initialize the first two Fibonacci numbers
octave:2> fib = zeros(1, n);
octave:3> fib(1) = 0;
octave:4> fib(2) = 1;
octave:5> % Calculate the Fibonacci sequence
octave:5> for i = 3:n fib(i) = fib(i-1) + fib(i-2); end
octave:6> % Display the Fibonacci sequence
octave:6> disp(fib);
    0    1    1    2    3    5    8   13   21   34

As we can see, octave uses octave:#> as its prompt.

In this instance, we calculate and display the first 10 numbers in the Fibonacci sequence, which is a series of numbers where each number is the sum of the two preceding ones.

The main advantage of octave is its sophisticated syntax that allows for complex expressions and control structures just like a scripting programming language.

6. Using wcalc

wcalc is a command-line calculator tool for Linux designed for quick and straightforward arithmetic calculations and expression evaluation. wcalc is lightweight and easy to use for basic mathematical operations.

Moreover, wcalc provides a wide range of constants, including mathematical constants like pi, physics constants, atomic and nuclear constants, and more.

First, let’s install wcalc:

$ sudo apt-get install wcalc

Then, we can use wcalc in shell mode:

$ wcalc
Enter an expression to evaluate, q to quit, or ? for help:
-> \radians 
Now Using Radians
-> x=45
display_val
xdisplay_val
 = 45
-> cos(x)/sin(x)*ln(4)
 = 0.855856

As we can see, wcalc uses -> as a prompt.

In this case, we input /radians to switch the calculator into radians mode. Then, we assigned 45 to the variable x. Finally, we evaluated a complex mathematical expression cos(x)/sin(x)*ln(4) with trigonometric functions and saw the result is 0.855856.

7. Using awk

awk is a powerful text processing tool available on the command line in Linux.

While awk isn’t a dedicated calculator, it can be used to perform mathematical calculations, manipulate data, and generate reports from text data.

Let’s see how we can use awk to perform calculations:

$ echo "10 4" | awk '{print $1 - $2}'
6
$ echo "3 7" | awk '{print $1 * $2}'
21
$ echo "12 4" | awk '{print $1 / $2}'
3

In this case, we used echo to generate text containing pairs of numbers. Then, we pipe that text to awk. In every awk command variant, we extract the numbers from the text, perform a specific mathematical operation on them, and then print the result.

As a full-fledged programming language, AWK can do much more than just calculate mathematical expressions.

8. Using Bash

We can perform calculations directly from the command line using a variety of methods. One of the most straightforward ways is by using double parentheses $((…)) for arithmetic operations.

Let’s see how we can use Bash for basic calculations:

$ echo $(( 10 * 15 ))
150

In this case, we used echo to display the result of the calculation. To do so, we entered a calculation within $((…)).

9. Using genius

genius is a powerful tool that can be used from the command line in Linux.

One of the notable features of the genius calculator is its support for arbitrary precision arithmetic. This means that it can handle very large numbers and perform calculations with extremely high precision.

First, let’s install genius:

$ sudo apt-get install genius

Then, we can use genius to calculate a large number:

$ genius
Genius 1.0.27
Copyright (C) 1997-2021 Jiří (George) Lebl
This is free software with ABSOLUTELY NO WARRANTY.
For license details type `warranty'.
For help type `manual' or `help'.

genius> x = 1234567890123456789012345678901234567890
= 1.23456789012e39
genius> x * 2
= 2.46913578025e39

Notably, genius uses genius> for a prompt.

In this case, the input is a large number multiplied by 2 and the result is 2.46913578025e39.

10. Conclusion

Linux offers a variety of command-line calculators, each with its own set of features and capabilities. Some calculators are designed for simple arithmetic operations, while others can handle more complex mathematical and scientific calculations.

In this article, we’ve explored various command line calculators available in Linux CLI, outlining their benefits and drawbacks.

Comments are closed on this article!