2

Here is my code

read a
read b
c=`echo $a / $b | bc`
echo "Answer is: " $c

I inputted values 10 and 3 to a and b respectively and i got the answer 3 in integer. So i put scale as below

read a
read b
c=`scale=2;echo $a / $b | bc`
echo "Answer is: " $c

But it gives an error "command not found"

So how is it possible? See i want to store the answer in the variable, as i know to display float calculations using echo only. I want the answer 3.33 stored in the variable $c.

1 Answer 1

3

You need to use quotes around scale=2 and your math expression:

a=10; b=3
c=$(echo "scale=2; $a / $b" | bc)    
echo "$c"

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

Comments

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.