Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to fetch the environment variables exported through subprocess

I have a shell script which exports some credentials.

BHUKRK848D:~ ranip.hore$ cat cred.ksh
export ORACLE_UID_MD="abcd"
export ORACLE_PWD_MD="welcome"

I am trying to execute the shell script from a python script using subprocess and on trying to get the exported variables using os.environ from the python script, I am unable to retrieve them.

Below is the code snippet I am executing :

BHUKRK848D:~ ranip.hore$ cat demo.py
import os
import subprocess
subprocess.call("sh cred.ksh",shell=True)
print(os.environ.get("ORACLE_UID_MD",None))

BHUKRK848D:~ ranip.hore$ python demo.py 
None

Is it due to the reason that the variables are exported in a different process and from script while fetching it is trying from some separate process? If i execute the export commands manually from terminal and run the python command it is able to fetch the credentials.

like image 219
raniphore Avatar asked Jan 22 '26 02:01

raniphore


1 Answers

Environment variables are inherited downwards by subprocess, but it doesn't propagate upwards to the parent process. Setting environment variables in a subprocess don't affect the environment variables of the parent process.

You'll have to parse the environment variables from the file instead of executing it.

like image 115
Lie Ryan Avatar answered Jan 23 '26 14:01

Lie Ryan