I am looking for a calculator which can do calculations in the terminal itself, without any other extra prefixes and suffixes.
For example: If I typed something like 10000-9000 in the terminal, the answer should come out as 1000.
Once again I am saying, I just need a quick calculator in the terminal, without any characters added. I know if I switch to Python, it can do that, but I don't want it in such a way.
You can use
calc
. Is not installed by default, but you can install it quickly using the following command:After you have installed, you can do any calculation do you wish:
For more information , view its man-page
You can do simple integer arithmetic natively in bash using the
((...))
syntax, e.g.There is also the
bc
calculator, which can accept arithmetic expressions on standard inputThe
bc
program can do floating point arithmetic as wellBash Arithmetic
Another possible solution is to add a simple function for Bash's built-in arithmetic. Put this in your
.bashrc
file to try it out: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+
andx
with*
. This will work for that: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 the0x...
hex syntax)Example:
Using
bc
If you want slightly more advanced calculations, you can pipe it to
bc
like so:The functions provided by
bc
are as follows (and can be found inman bc
):It also supports
if
,for
,while
, and variables like a programming language. Though it may be better to write to a file if you wanted that.Keep in mind that it will substitute
p
andx
in function/variable names. It may be better to just remove the replacements.Using
gcalccmd
You can also make the function call
gcalccmd
(fromgnome-calculator
) like so:The available functions seem to be (taken straight from the source code),
==
denotes equivalent functions: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 ifbc
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...
... 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.
I'd advise you to create a simple function for basic Python calculations. Something like this in your
.bashrc
: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)(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
,place it in a directory included in the
PATH
variable and set its executable flag to get the samecalc
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).
Use the
gcalccmd
fromgnome-calculator
(>=13.04) orgcalctool
(<13.04) package. I think the package is installed by defaultHere's a quick shell script for this:
Save this as "c", then put it somewhere in your path (such as /bin), then mark it executable.
From now on, you can run calculations in the terminal like this:
Another solution I haven't seen mentioned here is Qalculate (qalc).
for the CLI version,
for the GUI.
It has a bunch of features such as:
20 m / s * 12 h = 864 kilom
pi
,e
,c
,avogadro
sin(pi) = 0
,gamma(4) = 6
,5! = 120
,log(1024, 2) = 10
(x + y)^2 = x^2 + 2xy + y^2
integrate 3*x^2 = x^3
,diff sin(x), pi
help convert
,help integrate
factorial(5)
andfaculteit(5)
.You say you want to use it without prefixes, well... you can use it with a prefix:
$ qalc 5 ft + 3 cm (5 * foot) + (3 * centim) = 1.554 m
as well as running it as a REPL.
Here's a modification of the appropriate part of
/etc/bash.bashrc
(on Ubuntu 10.04) that will modify thecommand_not_found
handler to run the shell's expression evaluator if the first character of the unknown command is a number or-
or+
.You'll be able to do any shell arithmetic this way; see http://www.gnu.org/software/bash/manual/bashref.html#Shell-Arithmetic for a list of arithmetic operators.
Note that if the expression you want to evaluate contains a
*
, you will have to quote the*
with\
or quotes since the shell will do filename expansion before deciding which command to run. Same thing for other operators like>>
.Put this in your
~/.bashrc
, then type. ~/.bashrc
and try it out.Sample output: (I am typing
cta
, a typo, just to test that our new command_not_found handler will still try to look for unknown commands).dc
! It's part of coreutils, so it's installed on OS X, Ubuntu, and pretty much EVERYTHING else. It's an RPN calculator, so if you don't like those, it's not for you.Very basic commands are as follows (manpage has all the syntax that I didn't include. Exponentiation, anyone?)
You only need spaces between numbers. They are ignored in all other cases.
Typing a number pushes it to the top of the stack.