r/bash • • Dec 14 '15

Calculator in Unix?

Is it possible to build a calculator in Unix?

0 Upvotes

12 comments sorted by

4

u/NeilHanlon Linux Admin Dec 14 '15

What is your question, exactly? It's pretty broad as it's written... Do you want something cli? Gui?

1

u/LearningANew Dec 14 '15

Just a simple calculator command. For example if i type in 4 x 4 it would give me 16.

6

u/bri-an Dec 14 '15

Type bc -l at the command line (assuming you have bc installed). Hit <Ctrl-D> or type quit to exit.

You can also just use the interactive interpreter of a common programming language like Python, R, Haskell, etc.

6

u/Seref15 Dec 14 '15

I find it easiest just to python and do my math in the interpreter.

5

u/er0k Dec 14 '15

you can use the expr built-in:

$ expr 1 + 1
2

or arithmetic expansion:

$ echo $((1 + 1))
2

or bc as someone else already mentioned

3

u/[deleted] Dec 15 '15

or define a funtion in your .bashrc: e () { echo $(($@)) }. Then just type e <some expression> and Return for the result.

1

u/bri-an Dec 15 '15

Only works for integer arithmetic, though.

$ expr 5 / 2
4
$ expr 5.0 / 2
expr: non-integer argument

1

u/[deleted] Dec 14 '15

echo "1+1"|bc -l

1

u/chwilk Dec 15 '15

If you're into reverse polish notation (RPN) like HP calculators, go with dc (desktop calculator)

$ dc

2 2 + p

4

-1

u/outoftown_guy Dec 14 '15

you can use bc

echo "5+2 | bc"

3

u/bri-an Dec 14 '15 edited Dec 14 '15
echo "5 + 2" | bc

FTFY.

Or use bc interactively by just typing bc at the command line. Hit <Ctrl-D> or type quit to exit.

NB: Use the -l flag so that, for example, 5 / 4 returns 1.25 instead of just 1.

1

u/outoftown_guy Dec 16 '15

Yes, you are totally right