Might be worth looking in detail at the expansion.
When defining variables,
just about the only character that has an effect is $
.
Everything else is taken literally.
It's worth nothing that white space around the assignment operator (=
or :=
) is ignored.
foo := this|works
foo
is assigned the literal text this|works
.
Similarly,
baz := 'make|will|not|accept|this|with|the|single|quotes'
assigns the literal text 'make|will|not|accept|this|with|the|single|quotes'
to baz
.
Fine and dandy.
Now, when make decides to build this-fails-horribly
(possibly because you said to the shell make this-fails-horribly
)
it expands the block of commands before doing anything.
Not unreasonably,
$(whatIWant)
is replaced by "A string with its 'single|quoted|regex|alternatives'"
.
Again, fine and dandy.
What is left is passed verbatim, one line at a time, to the shell.
The shell sees
printf '"A string with its 'single|quoted|regex|alternatives'"'
(which make would have helpfully echoed to you if you had left off the @
prefix).
Now we are in the land of shell quoting.
- The
printf
command is passed one parameter: "A string with its single
:
'"A string with its '
is a single quoted string. The shell strips the '
s and is left with the text "A string with its
.
single
has no metacharacters in it, so the shell leaves this alone.
- The output is piped to the
quoted
command
- The output is piped to the
regex
command
- The output is piped to the
alternatives"
command
- The shell sees the single quoted string
'='
, strips the quotes leaving you with a literal =
which it appends to the word alternatives
No syntax error.
When the shell attempts to set up the pipeline it looks for the alternatives"
command.
It doesn't find one in the directories it its $PATH
, so it stops with the message /bin/sh: 1: /bin/sh: 1: regex: not foundalternatives": not found
.
One possible encoding:
.PHONY: this-workes-nicely
this-workes-nicely:
echo $(whatIWant)
though you'll probably find it's cleaner to leave the quotes outside the variable definition in the first place.