vehomzzz
vehomzzz

Reputation: 44618

how to perform a basic arithmetics from unix csh/tcsh shell

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

Upvotes: 5

Views: 70669

Answers (7)

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.

Upvotes: 20

William Pursell
William Pursell

Reputation: 212514

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

$ expr 1 + 1
2

expr has a sensible return value, so that it succeeds when the expression evaluates to a non-zero value allowing code (in a Bourne shell) like:

$ op="1 + 1"
$ if expr $op > /dev/null; then echo "$op is not zero"; fi
1 + 1 is not zero

or (if using a shell that supports arrays):

$ op=(8 \* 3)
$ if expr "${op[@]}" > /dev/null; then echo "${op[@]} is not zero"; fi
8 * 3 is not zero

Note that the if syntax in Bourne shells is completely different than in the csh family, so this is slightly less useful and you need to check against the value of #?.

Upvotes: 10

Sachin
Sachin

Reputation: 21921

Alternative option is to use the built in BC command

Upvotes: 0

DVK
DVK

Reputation: 129491

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

$ perl5.8 -e '$a=1+2; print "$a\n"' 
3

Upvotes: 0

monkut
monkut

Reputation: 43860

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()

Upvotes: 2

Mark Rushakoff
Mark Rushakoff

Reputation: 258388

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)

Upvotes: 4

alphazero
alphazero

Reputation: 27244

You can use dc. Or bc.

Upvotes: 10

Related Questions