193

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?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Jialin
  • 2,415
  • 2
  • 14
  • 10

5 Answers5

285

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.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 7
    "make imports all the environment variables you have set".It works! Nice~,I have no idea about this before,thank you very much. – Jialin Mar 07 '15 at 16:53
  • @jonathan_leffler I was inspired by your answer and I did something a bit different. I set the envvar before calling make without exporting it: `demoPath=/usr/local/demo make`. You might want to test it out and maybe updating your answer. Cheers! – A.D. Sep 18 '18 at 14:46
  • @A.D. Yes, that is another mechanism for setting a value for an environment variable for a specific command. It is slightly tangential to the question which asks how to access the value of an existing variable, rather than how to create one on the fly. It doesn’t stop it being a useful technique on occasion. – Jonathan Leffler Sep 18 '18 at 14:50
  • Be careful about some special variables that aren't imported automatically. For example, `$(SHELL)`, which determines what shell `make` uses to execute commands, is initialized to `/bin/sh` rather than the value from the environment. – Mark Reed May 25 '23 at 11:57
28
all:
    echo ${PATH}

Or change PATH just for one command:

all:
    PATH=/my/path:${PATH} cmd
g10guang
  • 4,647
  • 3
  • 28
  • 22
28

for those who want some official document to confirm the behavior

Variables in make can come from the environment in which make is run. Every environment variable that make sees when it starts up is transformed into a make variable with the same name and value. However, an explicit assignment in the makefile, or with a command argument, overrides the environment. (If the ‘-e’ flag is specified, then values from the environment override assignments in the makefile.

https://www.gnu.org/software/make/manual/html_node/Environment.html

Dyno Fu
  • 8,753
  • 4
  • 39
  • 64
  • 5
    The `-e` hint is gold! But you ommitted an important remark: "But this is not recommended practice" because now *any* variable can be overridden by the environment. – moi Mar 28 '21 at 08:52
16

if you export the variable in the same script you will need to use two $$ instead of $, if your Makefile is looking Something like this:

target:
        . ./init.sh; \
        echo ${HOMEPATH}:$${FOO};

in init.sh script you export the variable FOO

$ cat ./init.sh
#!/bin/bash
export FOO=foo:

this way when you run make target the env variable HOMEPATH that were defined before the script runs will be displayed only using one $, but the env variable FOO that is exported in init.sh script which is executed inside your make file will need $$ in order to be shown

Ali
  • 687
  • 8
  • 27
16

If your .env file is located in the same directory you can use include .env statement:

.env

DEMO_PATH=/usr/local/demo

Makefile

include .env

demo-path:
    echo ${DEMO_PATH}

Read more

Dmitry Grinko
  • 13,806
  • 14
  • 62
  • 86