在带有多个空格的makefile中打印变量

在带有多个空格的makefile中打印变量,makefile,Makefile,考虑以下简单的makefile: define HELP_MSG Usage:\n make help -show this message\n make help -show spaces and then this message\n endef export HELP_MSG help: @echo $$HELP_MSG 哪些产出: Usage: make help -show this me

考虑以下简单的makefile:

define HELP_MSG
  
Usage:\n
        make help -show this message\n
        make help                   -show spaces and then this message\n

endef
export HELP_MSG

help:
        @echo $$HELP_MSG
哪些产出:

Usage:
 make help -show this message
 make help -show spaces and then this message

如何让echo遵守第二个输出行上的额外间距?

您可以将-e与echo一起使用,并将tab用作空格。例如:

define HELP_MSG
  
Usage:\n
        make help -show this message\n
        make help \t-show spaces and then this message\n

endef
export HELP_MSG

help:
        @echo -e $$HELP_MSG
要添加自定义空格,请在echo中使用printf或“”。例如:

define HELP_MSG
  
Usage:\n
        make help -show this message\n
        make help           -show spaces and then this message\n

endef
export HELP_MSG

help:
    @echo -e "$$HELP_MSG"
    @printf '%s\n' "$$HELP_MSG"

使用
echo
打印格式化文本没有便携方式。
echo
-e
选项未标准化,并非所有版本的
echo
都支持
-e
。除了知道不以破折号开头的简单文本(
-
)外,您不应尝试打印任何内容。基本上,除了一个简单的静态字符串之外的任何东西

对于更复杂的内容,您应该使用
printf

此外,如果您想要打印非平凡的文本,您必须引用它,否则shell将解释它并为您将其搞乱

help:
        @printf '%s\n' "$$HELP_MSG"

好啊这使我能够打印选项卡。如果我想打印任意数量的空格怎么办?@rhz我已经编辑了任意数量空格的答案。