3

I need to add some real numbers in a script. I tried:

let var=2.5+2.5 

which gives me an error - invalid arithmetic operator and then I tried:

let var=2,5+2,5 

which does not give me an error, but it gives me a wrong result -2 in this case.

Why? How to add real numbers using let or other command?

user263207
  • 33
  • 1
  • 6
  • @RaduRădeanu this is exclusive to bash and other lesser shells, not all shells are affected, and OP is using bash. This has nothing to do with the usage of the CLI or any other shell. – Braiam Mar 29 '14 at 15:06
  • 3
    @Braiam Please try to not be so exclusivist. The OP used command-line tag probably in idea that him is interested by any command which will solve his problem. Furthermore, the OP accepted a non bash solution, a solution which present more command-line tools like bc, awk, etc.(please take a look at this answer). These tools are not exclusively related to bash! – Radu Rădeanu Mar 29 '14 at 15:24
  • @RaduRădeanu OP doesn't need know anything about how to use the tags, we do. [tag:command-line] are for questions about using the CLI and OP wants to know why a shell-builtin function doesn't work the way he expect in Bash. In zsh this would work just fine since they allow floating points operation and OP may be used to those shells. Ah, BTW, we don't tag question depending to answers, but to the questions – Braiam Mar 29 '14 at 15:28
  • Hey mods, please stop fighting from my question. I haven't completed higher education school of editing tags, I didn't know that is necessary here, but I want command-line tag, it's OK. – user263207 Mar 29 '14 at 16:31
  • If you use zsh you wouldn't see this behavior, so give it a try ;). – Braiam Mar 29 '14 at 16:53
  • What zzh? I wanted a command to use in a script. – user263207 Mar 29 '14 at 17:01
  • can you add the script you are working on? – Braiam Mar 29 '14 at 17:03

3 Answers3

6

The first variant (let var=2.5+2.5) doesn't work because bash doesn't support floating point.

The second variant (let var=2,5+2,5) works, but it may not be what you wish because comma has another meaning in this case: it's a separator, command separator. So, your command is equivalent in this case with the following three commands:

let var=2
let 5+2
let 5

which are all valid and because of this you get var=2.

But, the good news is that you can use bc to accomplish what you wish. For example:

var=$(bc <<< "2.5+2.5")

Or awk:

var=$(awk "BEGIN {print 2.5+2.5; exit}")

Or perl:

var=$(perl -e "print 2.5+2.5")

Or python:

var=$(python -c "print 2.5+2.5")
Radu Rădeanu
  • 169,590
4

Bash doesn't handle floating point arithmetic.

If you really need fp arithmetic, use bc or dc. E.g.,

var=$(bc <<< "2.5+2.5")
echo "$var"

will output 5.0, or with dc (lots of fun, it's reverse polish notation):

var=$(dc <<< "2.5 2.5 + p")
echo "$var"

will also output 5.0.

2

zsh is another shell different to bash that allows floating point in the shell. If you use zsh instead of bash, which do support let using floating points you don't need to modify the script. Just install it using sudo apt-get install zsh and then using it in your script with the shebang #!/usr/bin/zsh or using the shell. Demostration:

➜  ~  let var=2.5+2.5                      
➜  ~  echo $var
5.0000000000

The script should work fine since zsh implemented all the functions that bash has and more. Your second example will not work because the comma (,) is interpreted by the shells as separator. It tells to execute the command let with var=2 5+2 and 2.

Braiam
  • 67,791
  • 32
  • 179
  • 269
  • sudo apt-get install zsh sudo: unable to change to sudoers gid: Operation not permitted??? – user263207 Mar 29 '14 at 17:23
  • @user263207 is that system personal? – Braiam Mar 29 '14 at 17:25
  • It's a server from school but they assured me that only I have access to my account. – user263207 Mar 29 '14 at 17:30
  • @user263207 is likely you can install stuff on the system and that's why. You can use the other answers that uses stuff that are available in most Ubuntu versions. But, remember, bash doesn't support floating points or decimal numbers operations ;). – Braiam Mar 29 '14 at 17:34