In my program I want to let the user either run a shell command or execute a binary file. No matter how much I read about subprocess.Popen
I can't figure out a way to replicate the os.system()
functionality.
I simply want to echo $USER
but nothing seems to work.
Turning shell=True
..
>>> subprocess.Popen(['echo', '$USER'], shell=True,\
stdout=subprocess.PIPE,\
stderr=subprocess.PIPE).communicate()
>>> ('\n', '')
Passing current environment vars..
>>> subprocess.Popen(['echo', '$USER'], shell=True,\
env=os.environ\
stdout=subprocess.PIPE,\
stderr=subprocess.PIPE).communicate()
>>> ('\n', '')
So what's the proper way to run a shell command?
------- Edit: requirements ---------
Some people posted answers but maybe I should be more specific. I want to keep both stdout, stderr and return value stored somewhere. All answers seem to give either the one or the other.