• Solved Math in BASH Part 2: bc question

@daniel-lowrie87

I have a question concerning bc. In your video “Math in BASH Part 2”, at 25 minutes in, you go over the script: 3bc_script.sh

When var5 is assigned the value that results from running the bc commands, how does it know to return the sum of foo1 + foo2 instead of just the text “EOF”? It's not like you explicit assigned a value to EOF, and there's nothing special about EOF other than it's the last line of the bc command set.

If we go into bc and type out the same lines from the bc command set, when we get to the end and type EOF, it just returns a syntax error, but when it's scripted, it just works.

I'm guessing that bc has a “result” value when it exits, populated by the most recent calculation. If this is the case, why is the EOF text even necessary? Why isn't: “var5=\$(bc <<” on it's own, good enough?

Thanks for any insight you may have, and thanks for the great set of BASH videos you and Ronnie have put out.

-Chris

• 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

ITProTV
Show Host

• @daniel-lowrie87
Hi Dan,

OK, I see now. You are assigning an exit statement. It was clear that what you were doing in the video works, it just was bothering me as to HOW it works, I was assuming EOF, or shoe, or snorkel were variables that were getting the answer value assigned to it.

I suspected that it's something that is used in other shell programs and knowing what the mechanism is would be useful to know in case it is a feature of other shell programs, rather than it just being a quirky way that bc works.

You touched on it in the video, when you mentioned that you were using an advanced version of I/O redirection "<< ", and your example helps, and I see now that you comment that it is inline input redirection in the script, but it was still a little muddy. Luckily I have a co-worker that was able to expand on what you were doing there. I'm sure you would have come to similar explanation if I was more clear on what I was troubled with.

He told me that we are using a Here Tag and that bc complies with the Here Document convention, and yes that they are used with other shell programs.

Playing around with the idea I changed the script a bit, and it seems to be working.

var5=\$(bc << ExitHere
scale = 4
foo1 = ( \$var1 * \$var2 )
foo2 = ( \$var3 * \$var4 )
#foo1 + foo2
foo1 - foo2
ExitHere
#foo1 * foo2
#foo1 / foo2
)

Anyway, thanks again for the reply. Hopefully I've got it now.
-Chris

Posts 3Views 1685