Hey Chris! I see that you're having a bit of trouble understanding the last example of "math in bash" using the bc tool. Let's see if we can't remove the fog and clear things up.
I looks like the first thing to address is the way in which we are using bc. In the example (3bc_script.sh), we have declared 4 BASH variables right out of the gate. They are var1/2/3/4. Then we create var5 which is special because var5 is going to get its value from the output of the bc tool. Here, we get to use the bc tool with the input redirector and that is the first key to what is going on and why we have EOF being used. Let's look at the code...
var5=$(bc << EOF
I could have just as easily written this like this...
var5=$(bc << shoes
or
var5=$(bc << snorkel
All that EOF, or shoes, or snorkel is doing is telling bc that it has reached the end of the redirected input.
Then we start throwing bc syntax at bc...
scale = 4
foo1 = ( $var1 * $var2 )
foo2 = ( $var3 * $var4 )
foo1 + foo2
EOF
)
So here we have scale = 4, this is a special bc variable that truncates or extends the decimal place. In this case I've set it to 4 decimal places.
The next 2 lines are declaring variables in bc. bc is basically another scripting language that does math and in this little code block we are jumping into bc. I say this because, I don't want you to confuse the BASH variables with the bc variables (foo1 and foo2). So we are giving our bc variables some value and that value is the product of multiplying the values of our BASH variables $var1 * $var2 and $var3 * $var4 respectively. Now that our bc variables have value we can perform addition using those bc variables (foo1 + foo2).
Finally, we see our EOF. Now that we're done using bc for our math calculations, we need to jump out and EOF is how we do that. Again, this could be shoes or snorkel or anything else we want. Also don't forget to close your parenthesis for the var5 BASH variable.
Once we're out of bc and back into BASH, we can just echo the value of our var5 BASH variable, which gets its value from the bc script.
You can see this more simply by just using bc. Here is a modified version of the 3bc_script.sh script. Really the only difference is that we're not declaring BASH variables. At the terminal prompt just type...
bc << EOF
(You'll notice your prompt change. That's because you're now in bc)
>scale = 4
>foo1 = ( 4.56 * 5.67 )
>foo2 = ( 6.78 * 7.89 )
> foo1 + foo2
>EOF (This is to let bc know you're done and to jump back to BASH and display the answer to standard output)
79.3494 (Now you're back in BASH)
Hopefully that helps you out. If you're still having trouble please feel free to reply back with your questions. I'm happy to do all that I can to help you.
Cheers,
Daniel Lowrie