Adding two reals in shell

Solved
Hello,

I want to add two real numbers in a shell script. I've tried doing it in several ways, but none have produced a good result.

The only command that seems to work partially is the following:

addition='echo $var1 + $var2 | bc -l'


It displays the result with an error:

script.sh: line 40: 1.560: command not found 


Knowing that 1.560 is the expected result.

What do you think?

Configuration: Mac OS X / Safari 533.19.4

--
A dream leads to nothing, action leads to everything.
(J. FIAUX).

4 answers

  1. Contributor
    Hello,

    addition=$(echo "$var1 + $var2" | bc -l)

    ;-))

    --
    Zen my nuggets ;-)
    Take a step for the environment, close your windows and adopt a penguin.
    1
    1. It works very well for addition, but that's not the case if I do a division!!
      Is it possible to divide a real number by an integer in bash?
      0
    2. Contributor
      Ben yes :

      $ A="22.53";B="8" $ D=$(echo "$A/$B" | bc -l) $ echo $D 2.81625000000000000000 $
      0
    3. When we run the commands like that in line, yes it works, but in the script I have the same error as before: I get the result with command not found!
      0
    4. Contributor
      Show your script!
      0
    5. ping1="0.456" ping2="0.765" ping3="0.973" ping=$(echo "$ping1 + $ping2 + $ping3" | bc -l) ping=$(echo "$ping / 3" | bc -l)
      0