Expr: non-integer argument. How to subtract decimal numbers

I am trying to subtract two numbers by running the following bash script:

#!/bin/bash -x
cur_length=`cat length.txt`
cur_pos=`cat pos.txt`

diff=`$(expr $cur_length - $cur_pos)`
echo "$diff"

But the output says that expr has some problem:

+++ expr 235.68 - 145.9
expr: non-integer argument
+ diff=
+ echo ''

I searched for "expr: non-integer argument" on the net, but nothing included decimal numbers. How can I subtract such numbers? 235.68 - 145.9

Thanks in advance.

+4
source share
1 answer

Bash does not execute fractions, just integers. Use instead bc:

$ echo  '235.68 - 145.9' | bc
89.78

This result can, of course, be placed in a shell variable in the same way as you did with expr:

$ diff="$(echo  '235.68 - 145.9' | bc)"
$ echo $diff
89.78
+6
source

Source: https://habr.com/ru/post/1533274/


All Articles