9

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 os.environ.

I just cannot make it work. Here is my simple test.

#!/bin/bash

export myvar='first'

python - <<EOF
import os
os.environ["myvar"] = "second"
EOF

echo $myvar

I expected it to output second, however it still outputs first. What is wrong with my script? Also is there any way to pass variable without export?


summary

Thanks for all answers. Here is my summary.

A python script embedded inside bash will run as child process which by definition is not able to affect parent bash environment.

The solution is to pass assignment strings out from python and eval it subsequently in bash.

An example is

#!/bin/bash
a=0
b=0

assignment_string=$(python -<<EOF
var1=1
var2=2
print('a={};b={}'.format(var1,var2))
EOF
)

eval $assignment_string

echo $a
echo $b
user15964
  • 2,507
  • 2
  • 31
  • 57
  • Your python script can not change bash‘s environment (here variable myvar). – Cyrus Apr 23 '17 at 15:27
  • Why are you creating the bash script when you want to run a python code? Either use BASH syntax or Python. The thing which you require is not how programming works. – Moinuddin Quadri Apr 23 '17 at 15:36
  • 1
    Side note -- assuming you want your Python source code to be used as-is (not subject to parameter expansion, command substitution etc. by the bash shell) you must enclose the heredoc delimiter in quotes like `'EOF'`. That won't affect the code in your example, but this is a good habit to get into with heredocs. – Chris Johnson Apr 23 '17 at 15:52
  • Possible duplicate of [How to pass variables from python script to bash script](https://stackoverflow.com/q/4257098/608639) – jww Sep 23 '18 at 00:33

5 Answers5

4
  1. Unless Python is used to do some kind of operation on the original data, there's no need to import anything. The answer could be as lame as:

     myvar=$(python - <<< "print 'second'") ; echo "$myvar"
    
  2. Suppose for some reason Python is needed to spit out a bunch of bash variables and assignments, or (cautiously) compose code on-the-fly. An eval method:

     myvar=first
     eval "$(python - <<< "print('myvar=second')" )"
     echo "$myvar"
    
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
agc
  • 7,973
  • 2
  • 29
  • 50
3

Complementing the useful Cyrus's comment in question, you just can't do it. Here is why,

Setting an environment variable sets it only for the current process and any child processes it launches. os.environ will set it only for the shell that is running to execute the command you provided. When that command finishes, the shell goes away, and so does the environment variable.

You can pretty much do that with a shell script itself and just source it to reflect it on the current shell.

Community
  • 1
  • 1
Inian
  • 80,270
  • 14
  • 142
  • 161
2

There are a few "dirty" ways of getting something like this done. Here is an example:

#!/bin/bash

myvar=$(python - <<EOF
print "second"
EOF
)

echo "$myvar"

The output of the python process is stored in a bash variable. It gets a bit messy if you want to return more complex stuff, though.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
s-m-e
  • 3,433
  • 2
  • 34
  • 71
1

You can make python return val and pass it to bash:

pfile.py

print(100)

bfile.sh

var=$(python pfile.py)
echo "$var"

output: 100

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
hamid ghadery
  • 31
  • 1
  • 5
0

Well, this may not be what you want but one option could be running the other batch commands in python using subprocess

import subprocess
x =400
subprocess.call(["echo", str(x)])

But this is more of a temporary work around. The other solutions are more along what you are looking for. Hope I was able to help!

Anonymous Dodo
  • 261
  • 3
  • 17