49

How and when do I quote a string in a make file? What is best practice?

Is the following the way to quote?

$(warning  $(shell ls -ld "$(CURDIR)" ) )

I'm familiar with Bash where you usually quote variables to allow for embedded spaces. Do you do such in a makefile?

How should I do assignment statements with a string?

vara := "$(CURDIR)"

varb := $(CURDIR)

varc := /home/me/source

vard := "/home/me/source"

What about the space after the equal?

1 Answer 1

51

You should never quote anything because of make. Make doesn't understand or parse single- or double-quote characters in any way. Every quoting character you write in a makefile will be kept as a literal quote and passed along as-is to the commands that make invokes.

So, if the shell expects and can interpret a quoted string, then you should use quotes. Where the shell doesn't expect or won't correctly interpret a quoted string, you should not use quotes.

In your examples, whether the quotes are acceptable or not depends on how those variables are used. As above, make won't do anything special with quotes, which means that vard (for example) contains the literal string "/home/me/source" (including the quotes).

If you use that value in a way where the shell will handle the quotes for you, then it's fine:

all: ; echo $(vard)

will print /home/me/source (no quotes) because the shell interprets them. But if you use the variable in a make context, for example as a target or a prerequisite:

all: $(vard)
$(vard): ; echo $@

then this is not right, because the target and prerequisite are the literal strings "/home/me/source" (including the quotes).

In general it's best to not use quotes around filenames in variables, and instead add the quotes in the recipe around the make variable. Of course if the variable contains an entire shell script, not just a filename, then you should add appropriate quoting to the script.

4
  • ifequ "$(varx)" "a" was the partial source of confusion for me. Apr 28, 2014 at 21:13
  • 2
    One upshot of this is that make cannot abide spaces in filenames. Space is the list separator and—as MadScientist points out—is not quotable. make usually breaks up such a filename into useless fragments.
    – bobbogo
    Apr 30, 2014 at 12:52
  • 9
    might not be relevant to the OP's intent, but other users might land here and not understand the distinction between make variables like $(VARX) and shell variables like $${VARZ} - note the double dollar sign $$ which make uses to distinguish between them and so in the situation when you want to pass a shell variable to a command as a variable without expansion then you might want to use single quotes, EG: passing $ORIGIN to the linker, then use cc -Wl,-rpath='$${ORIGIN}' -shared -fPIC -Wall $(SRC) -o $(DIR)/$(LIB) with single quotes to keep it from expanding in the shell. Oct 2, 2017 at 18:13
  • regarding my comment, see MadScientists's answer on how to correctly pass $ORIGIN to the linker in a make file in which both single quotes and the double dollar sign are used, but the syntax of -Wl,option is slightly different than my example Oct 2, 2017 at 18:19

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.