$${HOME} or ${HOME} in Makefile?

2019-05-07 16:59发布

$ cat Makefile 
all:
    echo VAR is ${HOME}
    echo VAR is $${HOME}

Gives

$ make
echo VAR is /home/abc
VAR is /home/abc
echo VAR is ${HOME}
VAR is /home/abc

Why does echo VAR is ${HOME} syntax work in Makefile? I thought, to use shell variables you have to use $${HOME}}

1条回答
放荡不羁爱自由
2楼-- · 2019-05-07 17:18

Yes and no. It is best to use $$ to be explicit. However, there is a special rule for environment variables:

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. But 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. See section Summary of Options. But this is not recommended practice.)

查看更多
登录 后发表回答