【发布时间】:2017-03-03 23:12:29
【问题描述】:
我正在尝试为 GNU make 编写一个 Makefile。我无法弄清楚这里的问题是什么:
foo := this|works
bar := "I lost my 'single quotes'"
baz := 'make|will|not|accept|this|with|the|single|quotes'
whatIWant := "A string with its 'single|quoted|regex|alternatives'"
this-almost-works: #But the single quotes are lost.
@printf '%s ' "$(whatIWant)"
this-fails-horribly:
@printf '$(whatIWant)'
我收到以下错误消息
/bin/sh: 1: quoted: not found
/bin/sh: 1: /bin/sh: 1: regex: not foundalternatives": not found
blah blah Error 127
为什么它试图在 shell 中运行这个字符串的一部分?
如何定义一个变量以准确包含 whatIWant 的内容?
【问题讨论】:
-
用 '\' 转义你的管道,如果我没记错的话,管道是 makefile 中的特殊字符
-
酷。这有助于避免错误消息。虽然我真的不明白为什么 foo 有效而 baz 无效。我仍然需要保留那些单引号。由于某种原因,它们消失了。
-
Make 不解释引号,它们在字符串中。所以实际上你在这里做的是:
@printf '%s ' ""A string with its 'single|quoted|regex|alternatives'""用引号解释,所以'single|quoted|regex|alternatives'丢失它的引号,因为它是一个带引号的字符串。这有意义吗? -
管道在 makefile 中是 not 特殊字符,除了在 GNU make 先决条件列表中,它们将正常先决条件与仅订购先决条件分开。它们对 shell 来说是特殊的,但不在引号内(任何一种类型)。