how to perform a basic arithmetics from unix csh/tcsh shell
Solution 1
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()
Solution 2
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.
Solution 3
Solution 4
There are many good solutions given here, but the 'classic' way to do arithmetic in the shell is with expr.
Solution 5
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)
vehomzzz
He adapts a lazy approach to a complex learning. His eclectic personality coupled with eccentric character caused much harm to masses, and to himself.
Updated on January 30, 2020Comments
-
vehomzzz almost 4 years
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
-
SourceSeeker about 14 years
dc
if you prefer RPN (Forth-like),bc
if you prefer infix notation (C-like) -
Jubal about 14 yearsYes, use the interactive python interpreter :-). My Dave
-
alphazero about 14 yearsRight. (But RPN is a sacred geek creed; let us not show disrespect for it by saying it is like unto another, brother ;)
-
SourceSeeker about 14 yearsA true believer writes his prose subject object verb : "RPN, sacred geek creed, is."
-
DVK about 14 yearsThe 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
-
Dave Jarvis about 14 yearsSubject object verb a true believer his prose writes: "RPN, sacred geek creed, is."
-
ephemient about 14 yearsAlong these lines,
((...))
is similar but as a command (does not substitute result,$?
gets set), and$[...]
is short for$((...))
. -
t0r0X almost 8 years
$(( ... ))
is valid syntax for all SH derived shells, e.g. Bash, Zsh. See The Open Group Base Specifications Issue 7 -
Emiswelt over 5 yearsThis question was specific about tcsh/csh. What about systems that don't have python installed?
-
Emiswelt over 5 yearsThe question was specifically about tcsh/tcl.
-
norman_h almost 3 yearsthe question was about csh/tcsh. it was not about perl.
-
DVK almost 3 years@norman_h - would you mind please explaining the meaningful difference between running "perl" vs "bc" or "dc" command, neither of which are csh/tcsh builtins? Other than your Meta knowledge that Perl happens to be a powerful programming language, aside from being able to do a task like "bc" can? Also, accepted answer uses Python, so clearly that approach is perfectyly fine with OP.
-
DVK almost 3 yearsFor that matter,
expr
from another high voted asnwer is also NOT a csh/tcsh builtin but a separate command, likeperl
-
norman_h almost 2 yearsThe question explicitly heads up with tcsh/csh. Not all environments contain heavy solutions such as perl/python. Even bc/dc executables can be considered superfluous to a basic csh environment. I think of csh as c in a shell... and the current highest vote answer shows some of this syntax. FWIW, my current unix environment (that pays the bills) is tcsh and very light on with the extras, every day at work feels like its 1975.