python – running multiple bash commands with subprocess

python – running multiple bash commands with subprocess

You have to use shell=True in subprocess and no shlex.split:

import subprocess

command = echo a; echo b

ret = subprocess.run(command, capture_output=True, shell=True)

# before Python 3.7:
# ret = subprocess.run(command, stdout=subprocess.PIPE, shell=True)

print(ret.stdout.decode())

returns:

a
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 wasnt possible in my situation.

What I ended up doing is something like:

commands = 
echo a
echo b
echo c
echo d


process = subprocess.Popen(/bin/bash, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
out, err = process.communicate(commands)
print out

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.

python – running multiple bash commands with subprocess

Join commands with &&.

os.system(echo a > outputa.txt && echo b > outputb.txt)

Leave a Reply

Your email address will not be published. Required fields are marked *