GNU make 等同于 BSD make 的 $(var:Q)?
GNU make equivalent to BSD make's $(var:Q)?
BSD make有一个:Q
变量扩展修饰符,在the FreeBSD make man page中记录如下:
:Q Quotes every shell meta-character in the variable, so that it can be
passed safely through recursive invocations of make.
如果变量 var
的值为 a b\c"d'e$f
,则 $(var:Q)
扩展为 a\ b\c\"d\'e$f
(或等价物)。这有助于将字符串传递给 shell 而不必担心 shell 会解释任何特殊字符。
GNU make 是否有等价物?还是我必须自己转义特殊字符?
GNU make 提供了函数subst
和patsubst
可以帮助解决问题。这些更 一般 ,但需要开发人员做更多的工作,因为它们不能解决 特定的 问题。此外,文档没有显示他们使用正则表达式,添加到工作中。
例如,原则上您可以构建如下表达式:
$(subst \,\\,$(subst ",\", $(subst ',\', var)))
更多讨论,
- Escaping comma and space in GNU Make
- Can GNU make handle filenames with spaces?
- GNU Make, double quotes and lists
对于 sh 变体,只需将表达式括在单引号中,将任何嵌入的单引号更改为 '"'"'
.
quote = '$(subst ','"'"',)'
用法:
$(error [$(call quote,ab'c\ d$$f)])
脚注:无法在单引号内引用任何内容。因此,第二个单引号结束了被引用的表达式。因此,要处理嵌入的单引号,请用 '
关闭单引号,添加带引号的单引号 "'"
,开始另一个单引号字符串 '
.
BSD make有一个:Q
变量扩展修饰符,在the FreeBSD make man page中记录如下:
:Q Quotes every shell meta-character in the variable, so that it can be
passed safely through recursive invocations of make.
如果变量 var
的值为 a b\c"d'e$f
,则 $(var:Q)
扩展为 a\ b\c\"d\'e$f
(或等价物)。这有助于将字符串传递给 shell 而不必担心 shell 会解释任何特殊字符。
GNU make 是否有等价物?还是我必须自己转义特殊字符?
GNU make 提供了函数subst
和patsubst
可以帮助解决问题。这些更 一般 ,但需要开发人员做更多的工作,因为它们不能解决 特定的 问题。此外,文档没有显示他们使用正则表达式,添加到工作中。
例如,原则上您可以构建如下表达式:
$(subst \,\\,$(subst ",\", $(subst ',\', var)))
更多讨论,
- Escaping comma and space in GNU Make
- Can GNU make handle filenames with spaces?
- GNU Make, double quotes and lists
对于 sh 变体,只需将表达式括在单引号中,将任何嵌入的单引号更改为 '"'"'
.
quote = '$(subst ','"'"',)'
用法:
$(error [$(call quote,ab'c\ d$$f)])
脚注:无法在单引号内引用任何内容。因此,第二个单引号结束了被引用的表达式。因此,要处理嵌入的单引号,请用 '
关闭单引号,添加带引号的单引号 "'"
,开始另一个单引号字符串 '
.