GNU makefile how to and when to quote strings

前端 未结 1 548
灰色年华
灰色年华 2020-12-03 06:50

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)\" )          


        
相关标签:
1条回答
  • 2020-12-03 07:26

    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.

    0 讨论(0)
提交回复
热议问题