Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

environment variable set in make not seen by all sub-shells

I need to set env variables in a makefile and use them in a python script invoked by the makefile. For some reason the python script is not able to see these env variables.

export VAR := env_var
PY_VAR := python -c "import os; print('VAR=' + str(os.environ.get('VAR')))"

all:
    @echo -n "echoing: VAR="
    @echo $${VAR}
    @echo -n "printenv: "
    @printenv | grep VAR
    @echo -n "python: "
    @echo $(shell ${PY_VAR})

Output:

echoing: VAR=env_var
printenv: VAR=env_var
python: VAR=None

What am I missing?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thank you,

>Solution :

In most versions of GNU make, variable exported from the makefile are passed to recipes but they are not passed to the shell function.

Only in the very latest version of GNU make (4.4 and above), you will see exported variables passed to shell.

In general it’s wrong / a bad idea to use shell in a recipe. It’s totally unnecessary because a recipe is already running in a shell.

If you run PY_VAR explicitly rather than using make’s shell function, it will work:

PY_VAR := python -c "import os; print('VAR=' + str(os.environ.get('VAR')))"

all:
        @echo $$(${PY_VAR})
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading