Nanor Nanor - 5 months ago 30
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:

./ 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)

# Bash
TABLE_NAME=$(./ FEED-NAME 2017-09-07)
echo "Table name is $TABLE_NAME"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download