If I run echo a; echo b
in bash the result will be that both commands are run. However if I use subprocess then the first command is run, printing out the whole of the rest of the line.
The code below echos a; echo b
instead of a b
, how do I get it to run both commands?
import subprocess, shlex
def subprocess_cmd(command):
process = subprocess.Popen(shlex.split(command), stdout=subprocess.PIPE)
proc_stdout = process.communicate()[0].strip()
print proc_stdout
subprocess_cmd("echo a; echo b")
I just stumbled on a situation where I needed to run a bunch of lines of bash code (not separated with semicolons) from within python. In this scenario the proposed solutions do not help. One approach would be to save a file and then run it with
Popen
, but it wasn't possible in my situation.What I ended up doing is something like:
So I first create the child bash process and after I tell it what to execute. This approach removes the limitations of passing the command directly to the
Popen
constructor.Join commands with "&&".
If you're only running the commands in one shot then you can just use
subprocess.check_output
convenience function:so, the problem is shlex module do not handle ";"
You have to use shell=True in subprocess and no shlex.split:
returns: