#!/bin/bash # Invoking 'bc' using command substitution # in combination with a 'here document'. var1=`bc << EOF 18.33 * 19.78 EOF ` echo $var1 # 362.56 # $( ... ) notation also works. v1=23.53 v2=17.881 v3=83.501 v4=171.63 var2=$(bc << EOF scale = 4 a = ( $v1 + $v2 ) b = ( $v3 * $v4 ) a * b + 15.35 EOF ) echo $var2 # 593487.8452 var3=$(bc -l << EOF scale = 9 s ( 1.7 ) EOF ) # Returns the sine of 1.7 radians. # The "-l" option calls the 'bc' math library. echo $var3 # .991664810 # Now, try it in a function... hypotenuse () # Calculate hypotenuse of a right triangle. { # c = sqrt( a^2 + b^2 ) hyp=$(bc -l << EOF scale = 9 sqrt ( $1 * $1 + $2 * $2 ) EOF ) # Can't directly return floating point values from a Bash function. # But, can echo-and-capture: echo "$hyp" } hyp=$(hypotenuse 3.68 7.31) echo "hypotenuse = $hyp" # 8.184039344 exit 0