Skip to content
Advertisement

Why does my python return value include previous print text?

I have created a small python script which I call from another shell script to calculate average value from the data in a file and I’m returning this average value back to a shell script variable. Here’s my code:

JavaScript

However, when I print the value stored in the shell variable

JavaScript

it shows the previous print text as well!

Average of all weights = 78.22 kg

78.22

Why does this happen? Am I making any mistake in the way it’s returning the averaged value? How can I get only 78.22 in the shell variable?

Advertisement

Answer

The shell captures whatever is printed to standard output. Print that message to standard error if you don’t want it in avgVal.

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement