0

I am trying to assign 2 variables floating values and then trying to store the sum in third variable but I get an error.

#!/bin/bash
x=0.1
y=1000.0
z=$((x+y))

echo $z

output is: xyz.sh: 6: xyz.sh: Illegal number: 0.1

I am unable to understand what is going wrong.

I want z to be = 1000.01

1 Answer 1

3

BASH doesn't support floating point arithmetic. Use bc command instead:

z=$(bc -l <<< "$x + $y")
echo "$z"
1000.1
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. I tried using bc and it worked out. #!/bin/bash x=0.1 y=1000.0 z= echo "$x+$y"|bc echo "$z"
I got stuck again. its actually not storing value in z but just printing it because i used z= echo "$x+$y"|bc when I used z=$(bc -l <<< "$x + $y") it says redirectionunexpected
using that syntax gives me a syntax error which say xyz.sh: 6: xyz.sh: Syntax error: redirection unexpected
Can you show you full script by editing the question. Make sure you are using bash not sh

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.