Preserve Environments Vars After Shell Script Finishes

Shell Script with environment variable

Environment variables are not an appropriate way of passing information from a process to the user or an invoking process. You should be writing this information to stdout (with any other info to stderr) and redirecting it appropriately.

If you insist on this solution, though, you can use

for p in "one arg" "another arg" "more"
do
echo 'printf "%s\n" "$TEST" >> test.txt' | ./cprog "$p"
done

This will input the command to write the variable to the file to your program, and then exit the shell (because input ends).

At the end, you'll have all the values of $TEST concatenated in the file test.txt.

Global environment variables in a shell script

Run your script with .

. myscript.sh

This will run the script in the current shell environment.

export governs which variables will be available to new processes, so if you say

FOO=1
export BAR=2
./runScript.sh

then $BAR will be available in the environment of runScript.sh, but $FOO will not.

How to check if an environment variable exists and get its value?

[ -z "${DEPLOY_ENV}" ] checks whether DEPLOY_ENV has length equal to zero. So you could run:

if [[ -z "${DEPLOY_ENV}" ]]; then
MY_SCRIPT_VARIABLE="Some default value because DEPLOY_ENV is undefined"
else
MY_SCRIPT_VARIABLE="${DEPLOY_ENV}"
fi

# or using a short-hand version

[[ -z "${DEPLOY_ENV}" ]] && MyVar='default' || MyVar="${DEPLOY_ENV}"

# or even shorter use

MyVar="${DEPLOY_ENV:-default_value}"

How to get the environment variable set by a bash script from inside a python script?

The easiest solution is clearly the one you don't want, which is to create a new script file for each python script.

You could, however, do roughly the equivalent by having the python script call itself. Of course, you need to signal it to not do that on the second invocation, otherwise you'll end up with an infinite (tail) recursion.

The following little "module" (which you can just import, but you should do it right at startup, before anything else) will check to see if the environment variable SETENV has been set, and if so, it will re-issue the python command (to the best of its ability, so it might get things wrong if it wasn't just a simple script execution) after sourcing the file named by SETENV. It lacks lots of error-checking and shouldn't be considered production-ready; rather a proof-of-concept:

# file env_set.py
import os
import sys

if sys.argv[0] and "SETENV" in os.environ:
setenv = os.environ["SETENV"]
del os.environ["SETENV"]
os.execvp("bash", ["bash", "-c",
"source " + setenv + "; exec python " + sys.argv[0] + ' "${@}"',
"--"] + sys.argv[1:])

And a little test:

# file test_env_set.py
import env_set

import os
import sys
for name in sys.argv[1:]:
if name in os.environ:
print(name + "=" + os.environ[name])
else:
print("Undefined: " + name)

# file setenv.sh
export the_answer=42

$ python test_env_set.py SETENV the_answer
Undefined: SETENV
Undefined: the_answer
$ SETENV=setenv.sh python test_env_set.py SETENV the_answer
Undefined: SETENV
the_answer=42

Environment variables getting preserved in Python script even after exiting

A subshell can change variables it inherited from the parent, but the changes made by the child don't affect the parent.

When a new subshell is started, in which the variable exported from the parent is visible. The variable is unsetted by del os.environ['var'], but the value for this variable in the parent stays the same.



Related Topics



Leave a reply



Submit