Search code examples
makefilegrepescapingdollar-sign

How to escape double dollars in a makefile?


Consider a directory containing (only) the 3 files obtained by:

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

(and thus containing respectively foobar, $foobar, $$foobar).

The grep instruction:

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

filters the files (actually, the unique file) containing double dollars:

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

So far, so good. Now, this instruction fails within a makefile, e.g.:

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

leads to the errors:

$ 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

Hence my question: how to escape double dollars in a makefile?

Edit: note that this question does not involve Perl.


Solution

  • with following Makefile

    a:
      echo '$$$$'
    

    make a gives

    $$
    

    ... and it's better to use single quotes if you do not need variable expansion:

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

    unless you write script to be able to be executed on Windows in MinGW environment, of cause.