Any command line calculator for Ubuntu?

Solution 1:

You can do simple integer arithmetic natively in bash using the ((...)) syntax, e.g.

$ echo $((10000-9000))
1000

There is also the bc calculator, which can accept arithmetic expressions on standard input

$ echo "10000-9000" | bc
1000

The bc program can do floating point arithmetic as well

$ echo "scale = 3; 0.1-0.09" | bc
.01

Solution 2:

You can use calc. Is not installed by default, but you can install it quickly using the following command:

sudo apt-get install apcalc

After you have installed, you can do any calculation do you wish:

$ calc 5+2
    7
$ calc 5-2
    3
$ calc 5*2          
    10
$ calc 5/2
    2.5
$ calc 5^2
    25
$ calc 'sqrt(2)' 
    1.4142135623730950488
$ calc 'sin(2)'
    0.9092974268256816954
$ calc 'cos(2)'
    -0.416146836547142387
$ calc 'log(2)'
    ~0.30102999566398119521
$ calc 'sqrt(sin(cos(log(2))))^2'
    ~0.81633199125847958126
$ # and so on...

For more information , view its man-page

Solution 3:

Bash Arithmetic

Another possible solution is to add a simple function for Bash's builtin arithmetic. Put this in your .bashrc file to try:

=() {
    echo "$(($@))"
}

So now, you don't even need $((...)) anymore, just = which seems natural enough.

Replacement

Another thing if you want to be even faster: you can make it replace p with + and x with *. This will work for that:

=() {
    local IFS=' '
    local calc="${*//p/+}"
    calc="${calc//x/*}"
    echo "$(($calc))"
}

= 5 x 5  # Returns 25
= 50p25  # Returns 75

Now you don't even need Shift anymore, the only thing is in front of arithmetic.

Hexadecimal output

Output can be displayed in both decimal and hexadecimal, if so desired. (Note: using x substitution will conflict with the 0x... hex syntax)

=() {
    local answer="$(($@))"
    printf '%d (%#x)\n' "$answer" "$answer"
}

Example:

$ = 16 + 0x10
272 (0x110)

$ = 16**3 + 16**4
69632 (0x11000)

Using bc

If you want slightly more advanced calculations, you can pipe it to bc like so:

=() {
    local IFS=' '
    local calc="${*//p/+}"
    calc="${calc//x/*}"
    bc -l <<<"scale=10;$calc"
}

= 'sqrt(2)' # Returns 1.4142135623
= '4*a(1)'  # Returns pi (3.1415926532)

The functions provided by bc are as follows (and can be found from man bc):

sqrt ( expression )
       The value of the sqrt function is the square root of the expression.  
       If the expression is negative, a run time error is generated.

s (x)  The sine of x, x is in radians.

c (x)  The cosine of x, x is in radians.

a (x)  The arctangent of x, arctangent returns radians.

l (x)  The natural logarithm of x.

e (x)  The exponential function of raising e to the value x.

j (n,x)
       The Bessel function of integer order n of x.

It also supports if, for, while and variables like a programming language though if it may be better to write to a file if you wanted that.

Keep in mind that it will substitute p and x in function/variable names. It may be better to just remove the replacements.

Using gcalccmd

You can also make the function call gcalccmd (from gnome-calculator) like so:

=() {
    local IFS=' '
    local calc="$*"
    # Uncomment the below for (p → +) and (x → *)
    #calc="${calc//p/+}"
    #calc="${calc//x/*}"
    printf '%s\n quit' "$calc" | gcalccmd | sed 's:^> ::g'
}

= 'sqrt(2)' # Returns 1.4142135623
= '4^4'     # Returns 256

The available functions seem to be (taken straight from the source code), == denotes equivalent functions:

ln()
sqrt()
abs()
int()
frac()
sin()
cos()
tan()
sin⁻¹() == asin()
cos⁻¹() == acos()
tan⁻¹() == atan()
sinh()
cosh()
tanh()
sinh⁻¹() == asinh()
cosh⁻¹() == acosh()
tanh⁻¹() == atanh()
ones()
twos()

Solution 4:

Unfortunately, there's no "easier" way to do this. The interactive python interface on the command line is the best suited for what you need, because unlike apcalc\, python is included in Ubuntu. I am not sure if bc is included still, however, python is the hands-down favorite for this stuff.

You can just run the interactive python interface on the command line, and then do math that way. You can use that as your calculator.

To do that, you open the terminal, type python, then hit the Enter button.

Then, in the python prompt that shows up, you can type your math in. For example, 10000 - 9000. The next line output is the result.


If you mean, though, something where you just load the terminal and can do this...

$ 10000 - 9000
1000
$

... then no there's no way to do this in just the terminal without anything else, because Bash doesn't handle numerical arguments like that.

Solution 5:

I'd advise you to create a simple function for basic Python calculations. Something like this in your .bashrc:

calc() {
    python3 -c 'import sys; print(eval(" ".join(sys.argv[1:])))' "$@"
}

calc 5 + 5
# Returns 10

result="$(calc 5+5)"
# Stores the result into a variable

If you want to do more advanced math, you can use the following one which imports all of the math module's functions. (see here for more info)

calc() {
    python3 -c 'from math import *; import sys; print(eval(" ".join(sys.argv[1:])))' "$@"
}

calc 'sqrt(2)'  # Needs quotes because (...) is special in Bash
# Returns 1.4142135623730951

result="$(calc 'sqrt(2)')"
# Stores the result into a variable

(Note: Because Python is a programming language, some things may seems strange, e.g. ** for powers of and % for modulo)

Alternatively you can create a Python script calc,

#!/usr/bin/python3
from math import *
import sys
print(eval(' '.join(sys.argv[1:])))

place it in a directory included in the PATH variable and set its executable flag to get the same calc command as above (no need to create a Bash function to run a Python script).

If you want a method in pure Bash, use steeldriver's answer. This answer is only really beneficial if you need the more advanced functions (i.e. from math), as Python is relatively slow compared to Bash.


I'm not sure if this breaks your "switch to python it can do that & I don’t want it in such a way." note, but you don't need to enter the interactive prompt and the result is accessible in Bash so this answer seems valid (to me, at least).