title: Pass variable from Python to Bash
body: <p>I am writing a bash script in which a small python script is embedded. I want to pass a variable from python to bash. After a few search I only found method based on <code>os.environ</code>.</p>
  
  <p>I just cannot make it work. Here is my simple test.</p>
  
  <pre><code>#!/bin/bash
  
  export myvar='first'
  
  python - &lt;&lt;EOF
  import os
  os.environ["myvar"] = "second"
  EOF
  
  echo $myvar
  </code></pre>
  
  <p>I expected it to output <code>second</code>, however it still outputs <code>first</code>. What is wrong with my script? Also is there any way to pass variable without <code>export</code>?</p>
  
  <hr>
  
  <p><strong>summary</strong></p>
  
  <p>Thanks for all answers. Here is my summary. </p>
  
  <p>A python script embedded inside bash will run as child process which by definition is not able to affect parent bash environment.</p>
  
  <p>The solution is to pass assignment strings out from python and <code>eval</code> it subsequently in bash.</p>
  
  <p>An example is </p>
  
  <pre><code>#!/bin/bash
  a=0
  b=0
  
  assignment_string=$(python -&lt;&lt;EOF
  var1=1
  var2=2
  print('a={};b={}'.format(var1,var2))
  EOF
  )
  
  eval $assignment_string
  
  echo $a
  echo $b
  </code></pre>