0
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
6
  • This is really a shell question. The return value to the shell is a small integer, not a string. Meanwhile, the $(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. Commented Nov 8, 2012 at 1:59
  • (PS, when you exit a Python instance with a non-integer value, I believe it logs the value to stderr and exits with 1. At least that's what sys.exit does. 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.) Commented Nov 8, 2012 at 2:01
  • Thanks, I get it now. Pretty much, the shell can only read from standard output. Commented Nov 8, 2012 at 2:17
  • Well, no, the shell can also read return values, it's just that reading return values and reading stdout are different things. Commented Nov 8, 2012 at 2:17
  • so if the shell can read return values, can't I store in a variable and then echo it to stdout? I understand it's inefficient but out of curiosity. Commented Nov 8, 2012 at 4:44

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.