# Tag Info

203

There are lots of options!!! Summary \$ echo "\$((20.0/7))" \$ awk "BEGIN {print (20+5)/2}" \$ 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 ...

29

There are many ways to calculate. For simple expressions you can use bash itself: echo \$((20+5)) or expr: expr 20 + 5 And for complex cases there is great tool bc: echo "20+5" | bc Btw, bc can calculate even very complex expression with roots, logarithms, cos, sin and so on.

21

The mentioned solutions are fine for very simple calculations, but very error-prone. Examples: # without spaces 20+5 literally produces 20+5 expr 20+5 → 20+5 # bc's result doesn't give the fractional part by default bc <<< 9.0/2.0 → 4 # expr does only integer expr 9 / 2 → 4 # same for POSIX arithmetic expansion echo \$((9/2)) → 4 # bash ...

17

echo 'ibase=16;C06D1000-C06A5E78' | bc 176520 if you need answer in hex too (but note that only upper case hex digits are supported as lower case ones would conflict with function and variable names, which is why you got 0 in your example (var1 - var2)): echo 'obase=16;ibase=16;C06D1000-C06A5E78' | bc 2B188 ps. FYI scale isn't designed for conversion ...

15

Nobody has mentioned awk yet? Using POSIX shell functions, and awk math power, just define this (one line) function: calc(){ awk "BEGIN { print \$*}"; } Then just execute things like calc 1+1 or calc 5/2 Note: To make the function always available, add it to ~/.bashrc (or your corresponding shell's startup file) Of course, a little script named "calc" ...

14

You could use bc. E.g., \$ echo "25 + 5" | bc 30 Alternatively bc <<< 25+5 will also work. Or interactively, if you want to do more than just a single simple calculation: \$ 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'. ...

14

dc is a very archaic tool and somewhat older than bc. To quote the Wikipedia page: It is one of the oldest Unix utilities, predating even the invention of the C programming language; like other utilities of that vintage, it has a powerful set of features but an extremely terse syntax. The syntax is a reverse polish notation, which basically means that ...

13

You can use calc: If you just enter calc with no other arguments it enters an interactive mode where you can just keep doing math. You exit this by typing exit: C-style arbitrary precision calculator (version 2.12.3.3) Calc is open software. For license details type: help copyright [Type "exit" to exit, or "help" for help.] ; 2+4 6 ; 3+5 8 ; 3.4+5 8.4 ; ...

10

I like to fire up Python and use it as an interactive calculator (but then again, I'm a Python programmer).

10

The substring inside the ` ` must be a valid command itself: rownum=`echo \$nextnum+1 | bc` But is preferable to use \$( ) instead of ` `: rownum=\$(echo \$nextnum+1 | bc) But there is no need for bc, the shell is able to do integer arithmetic: rownum=\$((nextnum+1)) Or even simpler in bash and ksh: ((rownum=nextnum+1))

10

Since no-one else has mentioned it, and though it's not strictly a calculator (but neither are all these general-purpose scripting languages), I'd like to mention units: \$ units "1 + 1" Definition: 2 \$ units "1 lb" "kg" * 0.45359237 / 2.2046226 Or, for less output so you can get just the number to use in \$() to assign to ...

8

bc supports the natural logarithm if invoked with the -l flag. You can calculate the base-10 or base-2 log with it: \$ bc -l ... l(100)/l(10) 2.00000000000000000000 l(256)/l(2) 8.00000000000000000007 I don't think there's a built-in factorial, but that's easy enough to write yourself: \$ bc ... define fact_rec (n) { if (n < 0) { print "oops"; ...

6

\$> ghc -e '20 + 5' 25 it :: Integer Also ghci, that is the Glasgow-Haskell Compiler in interactive mode (ghc --interactive, as opposed to it evaluating an expression with -e), makes for a fascinating "calculator": \$>ghci GHCi, version 7.8.3: http://www.haskell.org/ghc/ :? for help Loading package ghc-prim ... linking ... done. Loading package ...

5

It is interpreting C and A as units. (Coulombs and Amperes.) The button where you selected “Hexadecimal” only applies when displaying the result. It doesn't affect the way the expression that you enter is parsed. Try 0xC - 0xA.

4

I have found a solution. calc () { awk ' function asin(x) { return atan2(x, sqrt(1-x*x)) } function acos(x) { return atan2(sqrt(1-x*x), x) } function atan(x) { return atan2(x,1) } function tan(x) { return sin(x)/cos(x) } BEGIN { pi=atan(1)*4; print '"\$(echo "\$@" | tr , .)}" | tr . , } This one accepts numbers as 5,2 or 5.2 (i.e. ...

4

There is no reliable way to implement that. The issue is the comma is used to separate arguments when more that one is passed to a function. If you aren't going to use such functions, I guess the simplest way would be something like: calc () { echo "scale=6;" "\$@" | tr , . | bc -l | tr . , } That gives: \$ calc 1 + 1 2 \$ calc 1 / 3 ,333333 \$ calc ...

4

I use a little python script that will evaluate a python expression and print the result, then I can run something like \$ pc '[i ** 2 for i in range(10)]' [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] the script is: #!/usr/local/bin/python3 import sys import traceback from codeop import CommandCompiler compile = CommandCompiler() filename = "<input>" ...

4

A basic difference between the two is that dc uses the reverse Polish notation. It requires explicit commands even in order to produce an output. You might add two integers in bc by saying: bc <<< "2+4" and it would produce 6 on a line by itself. However, in dc you'd need to say: dc <<< "2 4 +p" You can also do much fun stuff using ...

4

As in intermixing arrays and stacks. In the example register a is used both as an array and a as a stack. 1 0:a 0 Sa 2 0:a La 0;a p First :a - register a is treated as an array. Then Sa - register a is treated as a stack. In effect pushing the array from from pt 1. down and creating a new array. As by man: Note that each stacked instance of a ...

3

You have to remember that dc is a compiler - and a crazy old one at that. It's a kind of machine language - a stack-oriented calculator. It's pretty powerful, but its interface is not designed for you - it's designed to efficiently process the instructions that you write in some other language after all of the user-friendliness has been machine processed out ...

3

Gnuplot gnuplot - an interactive plotting program Follow the above link or type gnuplot form the prompt then help inside the gnuplot interpreter. Gnuplot is a program born to plot data, but can be used for calculation too. It offer the advantage that you can define functions and or use the built-in ones. echo "pr 20+5/2" | gnuplot # Lazy-note ...

3

I can't believe to read "the power of JavaScript" (but I had to upvote the answer for the other parts, except perl of course. Practically, for the simple cases where integer arithmetic is sufficient, I use the buildin \$((...)) and recommend it. Else, in almost all cases echo "..." | bc is sufficient. For some arithmetic operations like statistics, matrix ...

3

For console calculations, I use concalc. (sudo aptitude install concalc) After that, just type concalc and hit enter. It won't supply a prompt, but just type in the your calculation (no spaces) and hit enter, and on the next line, it'll give you the numeric value.

3

Orpie is the calculator for calculator and command line geeks. It emulates a modern HP RPN calculator, which is of course the only true way to calculate. If you are a calculator heretic, raised on TIs, Casios, and such, there are many RPN tutorials online with which you may begin your re-education. The Orpie manual will eventually be of some use to you, ...

3

You can also use built in arithmetic in bash rownum=\$((nextnum+1)) which would be slightly faster

2

You can control the scale that bc outputs with the scale=<#> argument. \$ echo "scale=10; 5.1234 * 5.5678" | bc 28.52606652 \$ echo "scale=5; 5.1234 * 5.5678" | bc 28.52606 Using your example: \$ bc <<< 'scale=2; 1.5 * 1.5' 2.25 You can also use the -l switch (thanks to @manatwork) which will initialize the scale to 20 instead of the ...

2

dc is a calculator whereas bc is an actual language. See their man pages. dc dc is a reverse-polish desk calculator which supports unlimited precision arithmetic. It also allows you to define and call macros. Normally dc reads from the standard input; if any command arguments are given to it, they are filenames, and dc reads and executes the ...

2

bash and GNU date #grab today's date in YYYYMMDD format today=\$(date +%Y%m%d) #grab date as of 3 months ago in YYYYMMDD format three_months_ago=\$(date +%Y%m%d --date='3 months ago') #now convert dates to "seconds since epoch" format, and then divide the difference by 60*60*24 to convert from seconds to days printf '%d\n' \$(( (\$(date --date=\$today +%s) - \ ...

2

In bash & fish use awk With zero configuration: \$ awk "BEGIN {print 10/3}" 3.33333 or after configuring your shell2,3: \$ calc 10/3 3.33333 awk is preinstalled on most Unix-like OSes and there are a few other reasons1 it's a good choice In zsh, ksh93 or yash, use echo \$ echo \$((10./3)) 3.3333333333333333 Notes Note 1: awk Pros and Cons ♥ ...

2

There is TiLP. I've used it with my TI-84+ SE: it allows you to manage applications, programmes and variables, and IIRC it also has a "view screen" feature — either it has a screenshot function, or you can just use import or something else to grab a screenshot of TiLP showing the screen.

Only top voted, non community-wiki answers of a minimum length are eligible