1

Let's assume we have a couple of files whose names need to be quoted when used in a shell:

echo A > 'a.$A'
echo B > 'b.$B'

And there is a Makefile with a rule that depends on these files:

all: a.$$A b.$$B
    cat $+

The above would work for the files with simple names but for the ones in the example it produces:

cat a.$A b.$B
cat: a.: No such file or directory
cat: b.: No such file or directory

It looks like the shell expands the $A and $B expressions to empty strings.

If I quote the variable as cat '$+', then the expansion is prevented but all prerequisites are passed as a single argument:

cat 'a.$A b.$B'
cat: 'a.$A b.$B': No such file or directory

The fact that it's a single argument is sort of expected according to the documentation:

The names of all the prerequisites […], with spaces between them.

The question is, is there a way to obtain all the prerequisites as individual arguments to be able to quote them?

1 Answer 1

2

With GNU Make, you can use a pattern substitution to surround each word in $+ with single quotes:

all: a.$$A b.$$B
        cat $(patsubst %,'%',$+)

This won’t work with prerequisites containing whitespace, but Make can’t really handle those anyway.

1
  • This is insufficient, you have to replace all embedded ' with '\'' as well. Commented Oct 18 at 6:03

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.