bash/Makefile 中的双美元符号是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26564825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the meaning of a double dollar sign in bash/Makefile?
提问by JohnTortugo
When inserting a shell script inside a Makefile we have (?) to use a double dollar sign ($$) to make reference to variables. Why is that so?
在 Makefile 中插入 shell 脚本时,我们有 (?) 使用双美元符号 ($$) 来引用变量。为什么呢?
for number in 1 2 3 4 ; do \
echo $$number ; \
done
回答by anubhava
As per gnu make official doc:
Variable and function references in recipes have identical syntax and semantics to references elsewhere in the makefile. They also have the same quoting rules: if you want a dollar sign to appear in your recipe, you must double it (‘$$'). For shells like the default shell, that use dollar signs to introduce variables, it's important to keep clear in your mind whether the variable you want to reference is a make variable (use a single dollar sign) or a shell variable (use two dollar signs).
配方中的变量和函数引用与 makefile 中其他地方的引用具有相同的语法和语义。它们也有相同的引用规则:如果你想在你的食谱中出现一个美元符号,你必须把它加倍('$$')。对于像默认 shell 这样使用美元符号引入变量的 shell,重要的是要清楚要引用的变量是 make 变量(使用单个美元符号)还是 shell 变量(使用两个美元符号) )。
So in short:
简而言之:
- makefile variable => use a single dollar sign
- shell variable => use two dollar signs
- makefile 变量 => 使用单个美元符号
- shell 变量 => 使用两个美元符号
回答by phi1010
Not directly applicable to this example -- except if the code shown is executed via $(shell ...)
instead of being a rule:
不直接适用于这个例子——除非显示的代码是通过$(shell ...)
而不是规则执行的:
With secondary expansion enabled, make might also interpret the double dollar itself in the second processing phase, when it occurrs in the prequisites list. (First phase: read file, set variables; second phase: find and invoke dependency targets, execute rules)
启用二级扩展后,当它出现在先决条件列表中时,make 也可能会在第二个处理阶段解释双美元本身。(第一阶段:读取文件,设置变量;第二阶段:查找和调用依赖目标,执行规则)
This is used to allow dynamically specifying dependency targets, when the variable with the targets name is only available later in the file.
当具有目标名称的变量仅在文件中稍后可用时,这用于允许动态指定依赖项目标。
See https://www.gnu.org/software/make/manual/html_node/Secondary-Expansion.html.
请参阅https://www.gnu.org/software/make/manual/html_node/Secondary-Expansion.html。