0

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
1
  • 2
    Integer is a set of natural numerrs (0,1,2,3,...) and negative of them (-1,-2,-3,...). Floating number is expressed with an integer value 0('mantissa') and 'exponent' which show place of decimal point in mantissa. The word "floating integer" is contradictional. Commented Oct 5, 2014 at 3:19

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, will do so once it permits me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.