0

by using echo command to calculate decimals like this one below:

$echo $((1+1+2.1))

will produces this error below

bash: 1+1+2.1: syntax error: invalid arithmetic operator (error token is ".1")

so, what should I do in order to have the result 4.1 ?

CuriousNewbie
  • 319
  • 4
  • 13
  • `$(( ... ))` is not an `echo` feature, it's a shell feature. It works the same way for *every* command, not just `echo`, and completes before `echo` -- or whatever other command -- is run. That said -- that shell feature *does not support floating point* (or otherwise non-integer) *arithmetic at all*. In addition to the linked duplicate, see [BashFAQ #22](https://mywiki.wooledge.org/BashFAQ/022). – Charles Duffy Jun 13 '20 at 04:49
  • echo ((1+1)+(2.1)) – Nabeel Khan Jun 13 '20 at 04:50
  • @NabeelKhan, pardon? That doesn't do math in any POSIX-compliant shell. You can see what it does in bash at https://ideone.com/QeZtQh – Charles Duffy Jun 13 '20 at 04:51
  • @CharlesDuffy sorry, I didn't know that. – CuriousNewbie Jun 13 '20 at 04:54
  • 1
    @AhiungLim sorry, use this: echo "(1+1)+(2.1)" | bc -l – Nabeel Khan Jun 13 '20 at 05:04
  • @NabeelKhan thanks Nabeel you code is useful, but it can be more simplify using `echo 1+1+2.1 | bc` , i was trying to see what's the `-l` option about but from what i can see is that `-l` adds more decimals for the result which has a lot of decimals. – CuriousNewbie Jun 13 '20 at 06:44

0 Answers0