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")
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 likebc
,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