4

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.

Denis Bitouzé
  • 520
  • 6
  • 18
  • Possibly related: http://stackoverflow.com/questions/1320226/four-dollar-signs-in-makefile – Frodon Nov 23 '15 at 14:52
  • 1
    You escape `$` in a makefile recipe by doubling it `$$`. See [Using Variables in Recipes](http://www.gnu.org/software/make/manual/make.html#Variables-in-Recipes). – Etan Reisner Nov 23 '15 at 14:54
  • Possible duplicate of [How to quote a perl $symbol in a makefile](http://stackoverflow.com/questions/13691180/how-to-quote-a-perl-symbol-in-a-makefile) – tripleee Nov 23 '15 at 15:33

1 Answers1

11

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.

Andrey Starodubtsev
  • 5,139
  • 3
  • 32
  • 46
  • But `grep -l -r --include '*.txt' '$$$$' .` (with single quotes and doubling each of the dollars) does *not* the job within a makefile for filtering the files containing double dollars. – Denis Bitouzé Nov 23 '15 at 15:09
  • 1
    The dollar sign is a metacharacter in `grep` so it needs to be backslash-escaped or included in a character class. Try `grep .... '\$$\$$' .` or `grep .... '[$$][$$]' .` where the doubled dollar signs are still required in order to escape it from Make. – tripleee Nov 23 '15 at 15:29
  • I'm sorry, of cause you have to omit single quotes so wildcard would work. Corrected an answer. – Andrey Starodubtsev Nov 23 '15 at 15:30
  • 1
    @tripleee, or use grep -F – Andrey Starodubtsev Nov 23 '15 at 15:33