title: Python run multiple lines of commands in the shell
body: <p>Suppose you have a bunch of commands as would be typed into a shell </p>
  
  <pre><code>export x=1
  cd foo/the\ bar/baz
  grep x y z
  cd "the/quoted path"
  ls -l
  </code></pre>
  
  <p>To run a single command you can do:</p>
  
  <pre><code>subprocess.run(['ls','l'])
  </code></pre>
  
  <p>But the commands aren't independent. Later commands depend on earlier commands, exported variables, etc. What is the pythonic way to run these commands as if these were lines of code in a shell script? Is there a way around using the shell=True "taboo"?</p>
  
  <pre><code># DOESN'T work, each run is independent:
  for c in cmds: 
      subprocess.run(c, shell=True)
  
  # Seems to work but is mashing everything into a giant string the best way?
  subprocess.run(';'.join(cmds), shell=True)
  </code></pre>