views:

1135

answers:

9

Under windows, when I need to perform a basic calculations, I use a built-in calculator. Now I would like to find out what is the common way if you only have a shell.

Thanks

+8  A: 

You can use dc. Or bc.

`dc` if you prefer RPN (Forth-like), `bc` if you prefer infix notation (C-like)
Dennis Williamson
Right. (But RPN is a sacred geek creed; let us not show disrespect for it by saying it is like unto another, brother ;)
A true believer writes his prose *subject* *object* *verb* : "RPN, sacred geek creed, is."
Dennis Williamson
*Subject object verb* a true believer his prose writes: "RPN, sacred geek creed, is."
Dave Jarvis
+3  A: 

Bash supports basic (integer only) arithmetic inside $(( )):

$ echo $(( 100 / 3 ))
33
$ myvar="56"
$ echo $(( $myvar + 12 ))
68
$ echo $(( $myvar - $myvar ))
0
$ myvar=$(( $myvar + 1 ))
$ echo $myvar
57

(example copied straight from the IBM link)

Mark Rushakoff
+1 And, I also use it for fixed-point non-integer questions by adding zeroes in the equation and inserting decimal points in my head. :)
Don Branson
Along these lines, `((...))` is similar but as a command (does not substitute result, `$?` gets set), and `$[...]` is short for `$((...))`.
ephemient
A: 

If you're using bash, here's a handy example of a small shell script that allows you to do calculations from the command line (including specifying precision for floating-point numbers):

http://www.novell.com/coolsolutions/tools/17043.html

Amber
It uses `bc`.
Dennis Williamson
Yes, it does. It just adds the ability to invoke it easily as single-line command as opposed to a program that then takes input.
Amber
+3  A: 

And you can always use the python interpreter, it's normally included in linux distros.

http://docs.python.org/tutorial/introduction.html#using-python-as-a-calculator

$ python
Python 2.6.2 (r262:71605, Apr 14 2009, 22:40:02) [MSC v.1500 32 bit (Intel)]
Type "help", "copyright", "credits" or "license" for more information.
>>> 2+2
4
>>> # This is a comment
... 2+2
4
>>> 2+2  # and a comment on the same line as code
4
>>> (50-5*6)/4
5
>>> # Integer division returns the floor:
... 7/3
2
>>> 7/-3
-3
>>> # use float to get floating point results.
>>> 7/3.0
2.3333333333333335

The equal sign ('=') is used to assign a value to a variable. Afterwards, no result is displayed before the next interactive prompt:

>>> width = 20
>>> height = 5*9
>>> width * height
900

And of course there's the math module which should solve most of your calculator needs.

>>> import math
>>> math.pi
3.1415926535897931
>>> math.e
2.7182818284590451
>>> math.cos() # cosine
>>> math.sqrt()
>>> math.log()
>>> math.log10()
monkut
Yes, use the interactive python interpreter :-). My Dave
jskaggz
A: 

I know this is technically not arithmetic from the shell, but most modern distributions come with ruby and IRB (interactive ruby shell) which is perfect for this sort of thing (assuming you know a bit of Ruby). Another alternative is invoking the python interpreter which starts in an interactive shell if you don't provide any arguments.

Bayard Randel
+2  A: 

From this web page (for csh and derivatives, since you asked):

% @ x = (354 - 128 + 52 * 5 / 3)
% echo Result is $x
Result is 174

and

% set y = (354 - 128 + 52 / 3)
% echo Result is $y
Result is 354 - 128 + 52 / 3

notice the different results.

Personally, I stick to /bin/sh and call awk or something (for maximal portability), or others have exhibited the bash approach.

dmckee
+5  A: 

There are many good solutions given here, but the 'classic' way to do arithmetic in the shell is with expr.

William Pursell
The only caveat is that, since "*" is a special character in shell, you need to escape it when doing multiplication with expr.E.g. $ expr 1 + 2 * 3 expr: syntax error $ expr 1 + 2 \* 3 7
DVK
A: 

You can also use Perl easily where bc or expr are not powerful enough:

$ perl5.8 -e '$a=1+2; print "$a\n"' 
3
DVK
I don't use much complexity in the expression above, other than needless gibberish that perl is known for
vehomzzz
Ahh... thus speaketh the expert... 63 questions and 16 answers. Next time try to contribute something more to SO than a comment filled with snide nastiness and juvenile unsubstanciated language-bashing?
DVK
A: 

Alternative option is to use the built in BC command

Sachin Chourasiya