how.wtf

Use Linux terminal as a calculator

· Thomas Taylor

Linux provides several methods for calculating expressions using the command line.

Using bc — basic calculator for command line

bc is a native Linux tool (originated on Unix systems in 1975) that stands for “basic calculator”. To use it, simply type bc followed by an expression. To quit, type quit.

 1> bc
 2Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006, 2008, 2012-2017 Free Software Foundation, Inc.
 3This is free software with ABSOLUTELY NO WARRANTY.
 4For details type `warranty'. 
 55 + 5
 610
 725 * 4
 8100
 9100 / 2 * 35
101750
112 * (2 * 5) + 15
1235
13quit

Additionally, variables, arrays, algebraic expressions, etc. can be used. Refer to the bc manual for more information.

Using expr

The expr command evaluates basic expressions.

1> expr 5 + 5
210
3> expr 25 \* 4
4100
5> expr 100 / 2 \* 35
61750

Keep in mind, it should be used for basic calculations. If something more advanced is required (like parenthesis), bc or arithmetic expansion may be more suitable.

Using arithmetic expansion

Arithmetic expansion evaluates an expression and substitutes the result. In combination with echo, it can be used as a calculator.

1> echo "$((5 + 5))"
210
3> echo "$((25 * 4))"
4100
5> echo "$((100 / 2 * 35))"
61750
7> echo "$((2 * (2 * 5) + 15))"
835

Using qalc

Qalc is a multi-purpose cross-platform desktop calculator. It provides versatility and supports many everyday needs: currency conversion, percent calculation, etc.

Install it on Ubuntu via apt:

1sudo apt install qalc

Example usage:

 1> sqrt(72)
 2
 3  sqrt(72) = 6 × √(2) ≈ 8.485281374
 4
 5> fibonacci(133) to hex
 6
 7  fibonacci(133) ≈ 0x90540BE2616C26F81F876B9
 8
 9> 5/3 + 3/7
10
11  (5 / 3) + (3 / 7) = 44/21 = 2 + 2/21 ≈ 2.095238095
12
13> 5 + 5
14
15  5 + 5 = 10
16
17> exit

#Linux  

Reply to this post by email ↪