Nanor Nanor - 24 days ago 10
Python Question

Creating an environment variable from Python not available in parent bash script

I'm calling a Python script from my bash script and I need the result from said Python scripts computation. I've tried to do it by setting an environment variable in several ways, one being the answer in this question and one being like so:

table_name = get_table_name(feed)
subprocess.Popen(['export', 'TABLE_NAME='+table_name], shell=True)


I call the Python script from my bash file like so:

./auto_load.py FEED-NAME 2017-09-07
echo "Table name is" ${TABLE_NAME}


Since the environment variable is created from a process triggered by the bash script, shouldn't it be available in the bash script?

Answer Source

Child processes cannot change the environment of their parent process. It is simply not possible.

You can achieve the effect you want by having the Python script output the table name and having the Bash script capture the output.

# Python
table_name = get_table_name(feed)
print(table_name)

# Bash
TABLE_NAME=$(./auto_load.py FEED-NAME 2017-09-07)
echo "Table name is $TABLE_NAME"