16

I have a string that represent a float:

echo $NUM
5.03

I need to multiply this number for MEGA. If I do it directly:

MEGA="1000"
result=$(($NUM*$MEGA))

I receive an error:

syntax error: invalid arithmetic operator (error token is ".03 * 1000")
1
  • 1
    use bc, and also the search function at the top of the screen. Commented Dec 15, 2015 at 10:15

1 Answer 1

23

Bash only has integers, no floats. You'll need a tool like bc to properly assign the value of result:

result=$(bc -l <<<"${NUM}*${MEGA}")

Or you can use awk:

result=$(awk '{print $1*$2}' <<<"${NUM} ${MEGA}")
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.