How do I execute multiple shell commands with a single python subprocess call?
Use semicolon to chain them if theyre independent.
For example, (Python 3)
>>> import subprocess >>> result = subprocess.run(echo Hello ; echo World, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) >>> result CompletedProcess(args=echo Hello ; echo World, returncode=0, stdout=bHellonWorldn)
But technically thats not a pure Python solution, because of
shell=True. The arg processing is actually done by shell. (You may think of it as of executing
/bin/sh -c $your_arguments)
If you want a somewhat more pure solution, youll have to use
shell=False and loop over your several commands. As far as I know, there is no way to start multiple subprocesses directly with subprocess module.