How to use Bash parameter substitution in a Makefile?

回眸只為那壹抹淺笑 提交于 2019-11-28 12:05:55

问题


I've the following Makefile where I'd like to use Bash parameter substitution syntax as below:

SHELL:=/bin/bash
Foo=Bar
all:
  @echo ${Foo}
  @echo ${Foo/Bar/OK}

However it doesn't work as expected, as the output of the second echo command is empty:

$ make
Bar
(empty)

Although it works fine when invoking in shell directly:

$ Foo=Bar; echo ${Foo/Bar/OK}
OK

How can I use the above syntax in Makefile?


回答1:


If you want the shell to expand the variable you have to use a shell variable, not a make variable. ${Foo/Bar/OK} is a make variable named literally Foo/Bar/OK.

If you want to use shell variable substitution you'll have to assign that value to a shell variable:

all:
        Foo='$(Foo)'; echo $${Foo/Bar/OK}

Note that we use the double-dollar $$ to escape the dollar sign so that make doesn't try to expand it.

I strongly recommend you don't add @ to your rules until you're sure they work. It's the single most common mistake I see; if people would just not use @ they could see the command make is invoking, and then they would better understand how make works.



来源:https://stackoverflow.com/questions/42462115/how-to-use-bash-parameter-substitution-in-a-makefile

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