Code_Ed_Student
Code_Ed_Student

Reputation: 1190

Setting variable to floating integer value

I am new to bash scripting. I am currently performing a simple arithmetic problem that involves a floating integer. I know that bash by itself does not do arithmetic with floating integers. So I am using the bc calculator tool. The only issue is the syntax. I am able to get results but not in the desired way. How can I assign $N_RESULTS the value of the math operation?

//I get arithmetic error with this syntax
NUM1=128.17333
let "N_RESULTS = ($NUM1 - 1) / 10 + 1" | bc -l
echo $N_RESULTS

_

//I get correct results if do something like this
NUM1=128.17333
echo "($NUM1 - 1) / 10 + 1" | bc -l

Upvotes: 0

Views: 1946

Answers (1)

EJK
EJK

Reputation: 12527

Try this:

NUM1=128.17333
N_RESULTS=$(echo "($NUM1 - 1) / 10 + 1" | bc -l)
echo $N_RESULTS

This is a slight variation on your 2nd syntax. 2nd line uses the $(command) syntax to assign the output of a command to a variable.

Upvotes: 3

Related Questions