0

I am looking for bash function suitable to do some simple arithmetic expressions on point values For intance

i=1
let "i_mod=${i}+1"; echo $i_mod
gives me 2

but if
i=1.0
let "i_mod=${i}+0.5";echo $i_mod
gives me an error instead of 1.5

or alternatively one of the sollution that has already been suggested in other topic:

i=1.0
echo "$(($i+1.0))"
-bash: 1.0+1.0: syntax error: invalid arithmetic operator (error token is ".0+1.0")
user3470313
  • 203
  • 1
  • 5
  • 1
    Does this answer your question? [How to do integer & float calculations, in bash or other languages/frameworks?](https://unix.stackexchange.com/questions/40786/how-to-do-integer-float-calculations-in-bash-or-other-languages-frameworks) – thanasisp Nov 03 '20 at 10:37
  • not exactly! I've tried one of the sollution proposed for BASH and it did no work ( I edited my post.. – user3470313 Nov 03 '20 at 10:46
  • The solution you have updated, says "(ksh93/zsh/yash, some bash)", not "bash". But there are many other examples into this post. For example: `echo "$i + 1.0" | bc` or `bc <<< "$i + 1.0"` is enough. You can use scale with `bc` also. You can use `awk`. etc. In general, the shell is not for arithmetics by its own. – thanasisp Nov 03 '20 at 11:11
  • I mean "floating point arithmetics" (correction to my last comment) For integers you can just use the bash arithmetic expansion `(( ))`. – thanasisp Nov 03 '20 at 11:22
  • thank you so much !! l=$(echo "$i + 1.0" | bc) works fine !! – user3470313 Nov 03 '20 at 11:27

0 Answers0