profiler
profiler

Reputation: 627

Bash, arithmetic expression + bc

I have small problem with use bc command in unix. I have two varaibles: variable1, variable2. The arithmetic expression looks like:

res=$$((($variable1*10)/$variable2)

I would like to round the result from two divided numbers. I think, the best solution will be using bc -l command + scale=X, but doesn't work.

res=$$(((echo "scale=2; $variable1*10)/$variable2" | bc -l)

I would like to get more exact result. Now, f.e., I have:

res = 10

But should be

res = 9.23

Upvotes: 0

Views: 234

Answers (1)

paxdiablo
paxdiablo

Reputation: 881383

What you currently have won't work simply because the parentheses in the bc expression are unbalanced. In addition, you appear to have way more $, ( and ) characters in there than you need.

Without those flaws, it works fine:

pax> num=923
pax> den=1000
pax> res=$(echo "scale=2; $num * 10 / $den" | bc -l)
pax> echo $res
9.23

Upvotes: 2

Related Questions