def hello():
return "hello world"
The above is my python code. In the shell I am entering:
fooshell=$(python -c 'import hello; hello.hello()')
but when I do
echo $fooshell
I am getting an empty line.
asked Nov 8, 2012 at 1:43
ilyas patanam
5,3722 gold badges31 silver badges33 bronze badges
1 Answer 1
Use print
fooshell=$(python -c 'import hello; print(hello.hello())')
If your hello() method itself doesn't print to stdout.
answered Nov 8, 2012 at 1:46
Calvin Cheng
36.7k40 gold badges121 silver badges171 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
default
$(foo)construction doesn't get the return value of foo, it gets what foo printed to standard output. Once you understand that, the Python part is simple: print the string instead of returning it.sys.exitdoes. But that doesn't matter in this case, because you're not returning/exiting anything; you're just calling a function, throwing away its result, and falling off the end of the program.)