命令替换不适用于makefile中的echo [英] command substitution doesn't work with echo in makefile
问题描述
这是一个简单的命令,可以在我的bash shell中正常工作:
Here's a simple command that works fine in my bash shell:
echo "Created at: $(date)" >> README.md
它将Created at: Wed Jan 24 10:04:48 STD 2018
附加到README.md.
It appends Created at: Wed Jan 24 10:04:48 STD 2018
to README.md.
但是,ii我在我的makefile文件中包含了相同的命令,其行为是不同的.
However, ii I include the same command in my makefile, the behavior is different.
makefile:
README.md:
echo "Created at: $(date)" >> README.md
运行make README.md
会将命令替换视为空字符串,如下所示:
Running make README.md
will treat the command substitution as an empty string like this:
echo "Created at: " >> README.md
在README.md后面附加的是Created at:
.
What's appended to README.md is Created at:
.
如何获取命令替换以在makefile中正确显示回声?
How do I get command substitution to output properly with echo in a makefile?
推荐答案
如果您希望Make调用的shell接收以下内容:
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
否则,将扩展Make的变量date
,这将成为echo
作为参数的内容,因为makefile中的$(date)
会扩展变量date
.
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
.
请注意,如果Make变量date
的定义如下,则它将按预期工作,而无需在规则中引用$
:
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
原因是变量date
(在规则的配方中使用)将由Make扩展为$(date)
,并将被传递到外壳程序.
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.
这篇关于命令替换不适用于makefile中的echo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!