Shell script to export environment variables in make

折月煮酒 提交于 2019-12-08 09:03:36

You could change the shell used in the makefile:

SHELL = /usr/bin/ksh # Or whatever path it's at

But it's probably a good idea to convert the script to something compatible with /bin/sh (ideally completely POSIX-compatible) if you want it to work smoothly on other platforms.

Beware, this will probably not work as intended: as each Makefile command is executed in its own subshell, sourcing myscript will modify only the local environment, not the whole Makefile's one.

example:

debug: setup
    @echo "*** debug"
    export | grep ENVVAR || echo "ENVVAR not found"                   #(a)

setup:
    @echo "*** setup"
    export ENVVAR=OK; export | grep ENVVAR || echo "ENVVAR not found" #(b)
    export | grep ENVVAR || echo "ENVVAR not found"                   #(c)

output:

$ make debug
*** setup
export ENVVAR=OK; export | grep ENVVAR || echo "ENVVAR not found" #(b)
export ENVVAR='OK'

export | grep ENVVAR || echo "ENVVAR not found"                   #(c)
ENVVAR not found

*** debug
export | grep ENVVAR || echo "ENVVAR not found"                   #(a)
ENVVAR not found

As you can see, ENVVAR is found only in command (b), but commands (a) and (b) are executed in a new, clean environment.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!