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?

0

1 Answer 1

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"
Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.