Greg
Greg

Reputation: 1803

How to divide variable by 10

In bash I'm trying to divide a variable by 10. If I do this it works:

  echo "scale=1; 125/10" | bc
  12.5

I'm now trying to do the same with variable in a script, so $RX = 125

echo "scale=1; $((Rx/10))" | bc

But the value I get out now is 12, not 12.5?

Upvotes: 1

Views: 426

Answers (1)

Kaz
Kaz

Reputation: 58598

The problem is that $(( ... )) is arithmetic expansion syntax being interpreted by Bash, and Bash doesn't have floating point or rational arithmetic.

What you probably want is:

echo "scale=1; $RX/10" | bc

That is, "interpolate" the value of RX in order to build an expression which is then evaluated by bc.

Case is important; Rx and RX are not the same symbol in Bash.

Upvotes: 5

Related Questions