How to get a shell environment variable in a makefile?

Jialin picture Jialin · Mar 6, 2015 · Viewed 134.6k times · Source

In shell when I enter

echo $demoPath

it prints

/usr/local/demo

How can I get the value of this variable $demoPath in a makefile?

Answer

Jonathan Leffler picture Jonathan Leffler · Mar 6, 2015

If you've exported the environment variable:

export demoPath=/usr/local/demo

you can simply refer to it by name in the makefile (make imports all the environment variables you have set):

DEMOPATH = ${demoPath}    # Or $(demoPath) if you prefer.

If you've not exported the environment variable, it is not accessible until you do export it, or unless you pass it explicitly on the command line:

make DEMOPATH="${demoPath}" …

If you are using a C shell derivative, substitute setenv demoPath /usr/local/demo for the export command.