# Command line calculators

You can do a lot of things inside a Terminal window, even use a calculator. These are some calculator programs.

## Table of Contents

## Bash arithmetic expansion

You can perform arithmetic calculations without any external program, just englobe the operation with a `$`

and double parentheses. You can also use `expr`

.

```
echo $((5+8))
```

```
expr 5 + 8
```

## wcalc

You can run `wcalc`

and a mathematical expression (use single quotes when adding the expression):

```
$ wcalc 'log(8)'
= 0.90309
```

Or you can only run `wcalc`

and you will enter into interactive mode.

```
$ wcalc
Enter an expression to evaluate, q to quit, or ? for help:
->
```

Type `wcalc -P`

and the number of decimals displayed for the results.

```
$ wcalc -P20 pi
= 3.14159265358979323846
```

Some examples of using `wcalc`

:

```
-> 2*3
= 6
```

```
-> sqrt(8)
= 2.82843
```

```
-> ln 5
= 1.60944
```

## bc

`bc`

allows to add calculations on a file and run `bc <file>`

. It also has an interactive mode and it can get the operations from stdin:

```
$ echo "3.3+5.6" | bc
8.9
```

```
$ 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'.
```

- You can exit from this mode by typing
`q`.

`bc -l`

includes a math library with several basic functions included, like `l(x)`

for the natural logarithm of ‘x’, or `s(x)`

for the sine of ‘x’ (‘x’ is in radians).

`bc`

has some predefined variables. One of them is ‘scale’ and it means the number of decimals displayed. You can modify it by typing `scale=<number>`

inside `bc`

interactive mode.

```
scale=2
1/3
.33
```

Some examples of using `bc`

:

```
4*(3+5)
32
```

```
3^2
9
```

```
# from decimal to binary
$ echo "obase=2; 3" | bc
11
```

## qalc

Powerful command line calculator (there is a GUI app called Qalculate!). Read its man page to find all the available features, but these are some examples. Start by running `qalc`

(with no parameters) for interactive mode (exit from interactive mode by running `exit`

) or `qalc '<calculation>'`

(`>`

symbol in the examples means we are running the command in interactive mode). :

- Basic calculations
`> 2+3 2 + 3 = 5`

`$ qalc '2*(5-2)' 2 × (5 − 2) = 6`

- Ecuations:
`> 5x + 17 = 3 ((5 × x) + 17) = 3 x = −14/5 = −2.8`

- Convert units:
`> 50MB to GB 50 × megabyte = 0.05 GB`

`> 300Mbit/s to MB/s 300 × (megabit / second) = 37.5 MB/s`

`> 5 minutes to hours 5 × minute ≈ 0.08333333333 h`

`> 5W to kW 5 × watt = 0.005 kW`

- Calculate time needed to download/upload a file:
`> 8GB / (300Mbit / second) (8 × gigabyte) / ((300 × megabit) / second) ≈ 3 min + 33.33333333 s`

- Calculate days:
`> "2023-01-01" - "now" "2023-01-01" − "2022-08-30T19:34:25" = 123 d + 5 h + 25 min + 34.409016 s`

`> days("today","2023-01-01") days("2022-08-30", "2023-01-01") = 124`

If you have any suggestion, feel free to contact me via social media or email.

Featured content: