How to escape double dollars in a makefile?
Asked Answered
A

1

5

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.

Abutilon answered 23/11, 2015 at 14:44 Comment(3)
Possibly related: #1320726Drama
You escape $ in a makefile recipe by doubling it $$. See Using Variables in Recipes.Giannini
Possible duplicate of How to quote a perl $symbol in a makefileBazooka
L
13

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.

Logway answered 23/11, 2015 at 14:56 Comment(4)
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.Wasting
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.Bazooka
I'm sorry, of cause you have to omit single quotes so wildcard would work. Corrected an answer.Logway
@tripleee, or use grep -FLogway

© 2022 - 2024 — McMap. All rights reserved.