Take the 2-minute tour ×

I've got some issues on scripting... if someone could help me, it would be really good !

My script has:

VISITS=$((WR + RD));

And I'm getting this error:

./calc_serv_demand.sh: line 12: 0.0895406: syntax error: invalid arithmetic operator (error token is ".0895406")

Can someone help me?

I think it's because the bash works only with integer... I need to use float values, though.

thanks in advance

Problem solved:

VISITS=$(echo $WR + $RD | bc); echo $VISITS


share|improve this question
add comment

5 Answers

up vote 5 down vote accepted

You can use bc to do your floating point calculations, i.e.

echo $WR + $RD | bc

and so on.

share|improve this answer
HUm... it works for the sum, it doesn't work well for the multiplication. It's weird because I've checked on bc manual and it should work ! Have a look: My script: echo $WR + $RD | bc &> VISITS; VISITS=$(echo $WR + $RD | bc); # works fine echo $VISITS SERVICE_DEMAND=$(echo $VISITS * $SERVICE_TIME | bc); # return a weird error echo $SERVICE_DEMAND Error: 0.0895406 3.42007 12.401 3.5096106 (standard_in) 1: syntax error (standard_in) 1: illegal character: K (standard_in) 1: illegal character: H (standard_in) 1: illegal character: T ... –  Alucard Jun 26 '10 at 16:22
@user: Since * has a special meaning for the shell you have to write either \\* or '*'. Also, you should directly assign the result of the calculation to a variable instead of outputting to file: VISITS=$(echo $WR + $RD | bc). –  Benjamin Bannier Jun 26 '10 at 16:31
@user368453: If you do an echo of an * it displays all the filenames in the current directory. Just put quotes around the formula and it will work: SERVICE_DEMAND=$(echo "$VISITS * $SERVICE_DEMAND" | bc) –  Dennis Williamson Jun 26 '10 at 16:32
Great!! Now it works !! Thanks everyone ... –  Alucard Jun 26 '10 at 16:37
quote your variables and you should be fine. echo "$WR+$RD" | bc –  ghostdog74 Jun 27 '10 at 0:36
add comment

Use bc to do float calculations in Bash.

share|improve this answer
add comment

You'll have to use an external program like bc to do floating-point math in your scripts.

Something like:

echo ($WR+$RD)*$SERVICE_DEMAND | bc

share|improve this answer
add comment

To set the precision (number of digits of the answer to the right of the decimal point), write:

SERVICE_DEMAND=`echo "scale=5; $VISITS * $SERVICE_DEMAND" |bc -l`
echo Service Demand = $SERVICE_DEMAND

This outputs:

Service Demand = 1.0744872

The scale=5 sets 5 digits of precision; the backquotes cause the contained expression to be evaluated and the ouput (from the bc -l) to be assigned to your variable.

share|improve this answer
Thanks But... It returned the following mistake: (standard_in) 2: syntax error –  Alucard Jun 26 '10 at 16:32
I just ran this again on GNU bash, version 4.0.35(1)-release (i586-suse-linux-gnu) –  Larry Morell Jun 16 '11 at 17:07
Alucard, Perhaps your cut-and-pasting introduced a \r\n. Try <code>dos2unix scriptname</code> on your copy. –  Larry Morell Jun 16 '11 at 17:15
add comment

Instead of using bc, consider switching to a better programming language. Bash is simply unsuited for mathematics.

share|improve this answer
add comment

Your Answer


