2

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?

Nanor
  • 2,400
  • 6
  • 35
  • 66

1 Answers1

1

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"
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • For what it's worth, my Python script outputs quite a lot so I amended the Bash slightly to be ````TABLE_NAME=`./auto_load.py FEED-NAME 2017-09-07 | grep databasename```` where `databasename` is the database part of the fully qualified location `databasename.tablename`. I'm sure a different method exists, such as `tail`. – Nanor Sep 26 '17 at 11:32