title: Sending multiple commands to a bash shell which must share an environment
body: <p>I am attempting to follow this answer here: <a href="https://stackoverflow.com/a/5087695/343381">https://stackoverflow.com/a/5087695/343381</a></p>
  
  <p>I have a need to execute multiple bash commands within a single environment.  My test case is simple:</p>
  
  <pre><code>import subprocess
  cmd = subprocess.Popen(['bash'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
  
  # Write the first command
  command = "export greeting=hello\n"
  cmd.stdin.write(command)
  cmd.stdin.flush() # Must include this to ensure data is passed to child process
  result = cmd.stdout.read()
  print result
  
  # Write the second command
  command = "echo $greeting world\n"
  cmd.stdin.write(command)
  cmd.stdin.flush() # Must include this to ensure data is passed to child process
  result = cmd.stdout.read()
  print result
  </code></pre>
  
  <p>What I expected to happen (based on the referenced answer) is that I see "hello world" printed.  What actually happens is that it hangs on the first <code>cmd.stdout.read()</code>, and never returns.</p>
  
  <p>Can anyone explain why <code>cmd.stdout.read()</code> never returns?</p>
  
  <p>Notes:</p>
  
  <ul>
  <li>It is absolutely essential that I run multiple bash commands from python within the same environment.  Thus, subprocess.communicate() does not help because it waits for the process to terminate.</li>
  <li>Note that in my real test case, it is not a static list of bash commands to execute.  The logic is more dynamic.  I don't have the option of running all of them at once.</li>
  </ul>