command substitution doesn't work with echo in makefile [duplicate]

大城市里の小女人 提交于 2019-12-18 09:29:23

问题


Here's a simple command that works fine in my bash shell:

echo "Created at: $(date)" >> README.md

It appends Created at: Wed Jan 24 10:04:48 STD 2018 to README.md.

However, ii I include the same command in my makefile, the behavior is different.

makefile:

README.md:
    echo "Created at: $(date)" >> README.md

Running make README.md will treat the command substitution as an empty string like this:

echo "Created at: " >> README.md

What's appended to README.md is Created at:.

How do I get command substitution to output properly with echo in a makefile?


回答1:


If you want the shell that Make invokes to receive the following:

echo "Created at: $(date)" >> README.md

Then, you need to escape the $ with another $ inside the rule:

README.md:
    echo "Created at: $$(date)" >> README.md

Otherwise, the Make's variable date is expanded and that will be what echo gets as argument, since $(date) in a makefile expands the variable date.


Out of intererst

Note that, if the Make's variable date is defined as below, it will however work as expected without quoting the $ in the rule:

date = $$(date)

README.md:
    echo "Created at: $(date)" >> README.md

The reason is that the variable date (used in the rule's recipe) will be expanded by Make to $(date) and that will be passed to the shell.



来源:https://stackoverflow.com/questions/48429075/command-substitution-doesnt-work-with-echo-in-makefile

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