Is it possible to do something like this (somehow set up the shell) so that in the terminal you can type expressions like a * b , a + b , a - b , a / c , where a and b are numbers, and what Is this expression evaluated?

Those. what could be done like this:

 dzmitry@mycomp:~$ 12 * 45 

And not running, for example, a python:

 dzmitry@mycomp:~$ py Python 3.4.3 (default, Oct 14 2015, 20:28:29) [GCC 4.8.4] on linux Type "help", "copyright", "credits" or "license" for more information. >>> 12 * 45 540 >>> 

Those. I do not use a calculator, but I use a command shell with a running python, and I want to use the command shell as a calculator, minimizing intermediate steps

  • one
    Easily. echo $((12 * 45)) or a=$((12 * 45)); echo $a a=$((12 * 45)); echo $a will print 540, and with variables, echo $(($a-10)) will print 530. Those. The @(( ... )) construct evaluates an expression with integers in brackets right in the shell. - avp
  • So this is a more laborious process than typing py , pressing Enter and calculating the expression - pynix
  • one
    In fact, a strange question for the time of universal multiwindow. Open another window and calculate there (I usually use bc (or Emacs Lisp)). And the given information about the shell is more applicable when writing scripts (instead of calling expr ) - avp

2 answers 2

Is it possible to somehow do this (somehow configure the shell)

Alas, "customize" is impossible. the shell is designed to execute commands, and not to calculate arithmetic expressions.

for such calculations you need a specialized program. for example (of the most common) bc, dc, etc.

bc

rounds the result to integers by default. using the scale=число command you can set the number of decimal places. in order not to enter this command each time, it makes sense to write it into the configuration file ~/.bcrc .

dc

This calculator uses reverse Polish notation and, accordingly, a stack. example:

 5 k #задать количество знаков после запятой 2 3 / p # поместить в стек 2 и 3, выполнить деление /, напечатать верхушку стека p .66666 # это результат 
     $ expr 12 + 45 / 15 - 76 \* 8 + 17 -576 
    • 1) Separate in this way, for example, 60 by 32 2) Still, we would like that there was no need to introduce anything additional, except as an expression - pynix
    • one
      Well, yes, only integer. Sad but true. As a bonus, there is an operation% giving the remainder of the division - rjhdby