There are lots of options!!!
Summary
$ echo $((20.0/7))
$ zcalc
$ bc <<< 20+5/2
$ bc <<< 'scale=4;20+5/2'
$ expr 20 + 5
$ calc 2 + 4
$ node -pe 20+5/2 # Uses the power of JavaScript, e.g. : node -pe 20+5/Math.PI
$ echo 20 5 2 / + p | dc
$ echo 4 k 20 5 2 / + p | dc
$ perl -E "say 20+5/2"
$ python -c "print 20+5/2"
$ python -c "print 20+5/2.0"
$ clisp -x "(+ 2 2)"
$ lua -e "print(20+5/2)"
$ php -r 'echo 20+5/2;'
$ ruby -e 'p 20+5/2'
$ ruby -e 'p 20+5/2.0'
$ guile -c '(display (+ 20 (/ 5 2)))'
$ guile -c '(display (+ 20 (/ 5 2.0)))'
$ slsh -e 'printf("%f",20+5/2)'
$ slsh -e 'printf("%f",20+5/2.0)'
$ tclsh <<< 'puts [expr 20+5/2]'
$ tclsh <<< 'puts [expr 20+5/2.0]'
$ sqlite3 <<< 'select 20+5/2;'
$ sqlite3 <<< 'select 20+5/2.0;'
$ echo 'select 1 + 1;' | sqlite3
$ psql -tAc 'select 1+1'
$ R -q -e 'print(sd(rnorm(1000)))'
$ r -e 'cat(pi^2, "\n")'
$ r -e 'print(sum(1:100))'
$ smjs
$ jspl
Details
You can use POSIX arithmetic expansion echo $((...))
:
$ echo $((20+5))
25
$ echo $((20+5/2))
22
ksh93
and zsh
do support floats there:
$ echo $((4*atan(1)))
3.14159265358979324
(in zsh
, you need zmodload zsh/mathfunc
to get the math functions like atan
above).
Interactively with zsh:
$ autoload zcalc
$ zcalc
1> PI/2
1.5708
2> cos($1)
6.12323e-17
3> :sci 12
6.12323399574e-17
Witch (t)csh:
% @ a=25 / 3; echo $a
8
In the rc
shell family, akanga
is the one with arithmetic expansion:
; echo $:25/3
8
Or bc
(see below for interactive mode):
Mnemonic: B est C alculator
$ bc <<< 20+5/2
22
$ bc <<< 'scale=4;20+5/2'
22.5000
bc interactive mode:
$ bc
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
5+5
10
2.2+3.3
5.5
Or Rush's solution, expr
(no interactive mode):
$ expr 20 + 5
25
$ expr 20 + 5 / 2
22
or DQdims's calc
(required sudo apt-get install apcalc)
:
$ calc 2 + 4
6
Or manatwork's solution, node
(interactive mode: node
; output function not needed):
$ node -pe 20+5/2 # Uses the power of JavaScript, e.g. : node -pe 20+5/Math.PI
22.5
Or Arcege's solution, dc
(interactive mode: dc
):
Which is even more fun since it works by reverse polish notation.
$ echo 20 5 2 / + p | dc
22
$ echo 4 k 20 5 2 / + p | dc
22.5000
But not as practical unless you work with reverse polish notation a lot.
Or Perl (interactive mode: perl -de 1
):
$ perl -E "say 20+5/2"
22.5
Or Python (interactive mode: python
; output function not needed):
$ python -c "print 20+5/2"
22
$ python -c "print 20+5/2.0"
22.5
Or if you have clisp installed, you can also use polish notation:
$ clisp -x "(+ 2 2)"
Or Marco's solution, lua
(interactive mode: lua
):
$ lua -e "print(20+5/2)"
22.5
PHP (interactive mode: php -a
):
$ php -r 'echo 20+5/2;'
22.5
Ruby (interactive mode: irb
; output function not needed):
$ ruby -e 'p 20+5/2'
22
$ ruby -e 'p 20+5/2.0'
22.5
Guile (interactive mode: guile
):
$ guile -c '(display (+ 20 (/ 5 2)))'
45/2
$ guile -c '(display (+ 20 (/ 5 2.0)))'
22.5
S-Lang (interactive mode: slsh
; output function not needed, just a ;
terminator):
$ slsh -e 'printf("%f",20+5/2)'
22.000000
$ slsh -e 'printf("%f",20+5/2.0)'
22.500000
Tcl (interactive mode: tclsh
; output function not needed, but expr
is):
$ tclsh <<< 'puts [expr 20+5/2]'
22
$ tclsh <<< 'puts [expr 20+5/2.0]'
22.5
SQLite (interactive mode: sqlite3
):
$ sqlite3 <<< 'select 20+5/2;'
22
$ sqlite3 <<< 'select 20+5/2.0;'
22.5
Various SQL's:
SQLite:
echo 'select 1 + 1;' | sqlite3
MySQL:
mysql -BNe 'select 1+1'
PostgreSQL:
psql -tAc 'select 1+1
_The options on mysql and postgres stop the 'ascii art' image !
R in plain mode - lets generate 1000 Normal random numbers and get the standard deviation and print it
$ R -q -e 'print(sd(rnorm(1000)))'
> print(sd(rnorm(1000)))
[1] 1.031997
R using the littler script - lets print pi squared
$ r -e 'cat(pi^2, "\n")'
9.869604
$ r -e 'print(sum(1:100))'
[1] 5050
PARI/GP, an extensive computer algebra system for number theory, linear algebra, and many other things
$ echo "prime(1000)"|gp -q
7919 // the 1000th prime
$ echo "factor(1000)" | gp -q
[2 3]
[5 3] // 2^3*5^3
$ echo "sum(x=1,5,x)" | gp -q
15 // 1+2+3+4+5
Javascript shells:
$ smjs
js> 25/3
8.333333333333334
js>
$ jspl
JSC: 25/3
RP: 8.33333333333333
RJS: [object Number]
JSC:
Good bye...
$ node
> 25/3
8.333333333333334
>
bc
, it may be worth your while to read about the-l
option in the man page. – glenn jackman Jun 14 '12 at 21:39