如何在 makefile 中转义双美元?

How to escape double dollars in a makefile?

考虑一个包含(仅)通过以下方式获得的 3 个文件的目录:

echo "foobar" > test1.txt
echo "$foobar" > test2.txt
echo "$$foobar" > test3.txt

(因此分别包含 foobar$foobar$$foobar)。

grep指令:

grep -l -r --include "*.txt" "\$\$" .

过滤包含双元的文件(实际上是唯一文件):

$ grep -l -r --include "*.txt" "\$\$" .
./test3.txt

到目前为止,还不错。现在,该指令在 makefile 中失败,例如:

doubledollars:
    echo "Here are files containing double dollars:";\
    grep -l -r --include "*.txt" "\$\$" . ;\
    printf "\n";\

导致错误:

$ make doubledollars
echo "Here are files containing double dollars:";\
grep -l -r --include "*.txt" "\\ . ;\
printf "\n";\

/bin/sh: -c: ligne 2: unexpected EOF while looking for matching `"'
/bin/sh: -c: ligne 3: syntax error: unexpected end of file
makefile:2: recipe for target 'doubledollars' failed
make: *** [doubledollars] Error 1

因此我的问题是:如何在 makefile 中转义 double 美元?

编辑:注意本题不涉及Perl。

与以下 Makefile

a:
  echo '$$$$'

make a 给出

$$

...如果不需要变量扩展最好使用单引号:

grep -l -r -F --include *.txt '$$' .

当然,除非您编写的脚本能够在 MinGW 环境中的 Windows 上执行。