使用多个空格打印生成文件中的变量



考虑以下简单的makefile:

define HELP_MSG

Usage:n
make help -show this messagen
make help                   -show spaces and then this messagen
endef
export HELP_MSG
help:
@echo $$HELP_MSG

输出:

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

如何使@echo尊重第二条输出线上的额外间距?

使用echo打印格式化文本没有可移植的方法。echo-e选项不是标准化的,并且不是所有版本的echo都支持-e。除了您知道不是以短划线(-(开头的简单文本之外,您永远不应该尝试打印其他内容。基本上,除了一个简单的静态字符串之外的任何东西。

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

此外,如果你想打印非琐碎的文本,必须引用它,否则shell会对它进行解释,并为你搞砸它。

help:
@printf '%sn' "$$HELP_MSG"

您可以将-e与echo一起使用,并使用tab来表示空间。例如:

define HELP_MSG

Usage:n
make help -show this messagen
make help t-show spaces and then this messagen
endef
export HELP_MSG
help:
@echo -e $$HELP_MSG

要添加自定义空格,请使用printf或""回声。例如:

define HELP_MSG

Usage:n
make help -show this messagen
make help           -show spaces and then this messagen
endef
export HELP_MSG
help:
@echo -e "$$HELP_MSG"
@printf '%sn' "$$HELP_MSG"

最新更新