I'm trying to add two doubles

sum=`echo $y1+$y2 | bc -l`

the above script gives me sum = -2.704405652. How do i resolve this issue?


You can do it with awk with this command:

sum=`echo|awk -v y1=$y1 -v y2=$y2 '{print y1+y2}'`

As suggested in comment awk can be rewritten on this way (to avoid echo)

sum=`awk -v y1=$y1 -v y2=$y2 'BEGIN {print y1+y2}'`
  • @BlackHeart, welcome. If you are fine with the answer you can upvote and/or accept it :) – Romeo Ninov Apr 8 at 13:51
  • 1
    You can change the block to BEGIN{... and save the echo :) – mickp Apr 8 at 14:36

Try this,

echo "$y1 $y2" | awk  '{print $1+$2}'

Just print the two values separated by space and add the first two fields with awk

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.