简体   繁体   English

bash/Makefile 中的双美元符号是什么意思?

[英]What is the meaning of a double dollar sign in bash/Makefile?

When inserting a shell script inside a Makefile we have (?) to use a double dollar sign ($$) to make reference to variables.在 Makefile 中插入 shell 脚本时,我们必须 (?) 使用双美元符号 ($$) 来引用变量。 Why is that so?为什么?

for number in 1 2 3 4 ; do \
    echo $$number ; \
done

As per gnu make official doc : 根据gnu制作官方文档

Variable and function references in recipes have identical syntax and semantics to references elsewhere in the makefile. 配方中的变量和函数引用与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). 对于像默认shell这样使用美元符号来引入变量的shell,重要的是要清楚地记住要引用的变量是make变量(使用单个美元符号)还是shell变量(使用两个美元符号) )。

So in short: 简而言之:

  • makefile variable => use a single dollar sign makefile variable =>使用单个美元符号
  • shell variable => use two dollar signs shell variable =>使用两个美元符号

$$ is the PID of your current process. $$ 是您当前进程的 PID。

$ ps -ef|grep $$
user       208465  200620  0 10:30 pts/4    00:00:00 bash

$ for number in 1 2 3 4 ; do \
    echo $$number ; \
done
208465number
208465number
208465number
208465number

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. 启用二级扩展后,make可能还会在第二个处理阶段解释双美元本身,当它出现在先决条件列表中时。 (First phase: read file, set variables; second phase: find and invoke dependency targets, execute rules) (第一阶段:读取文件,设置变量;第二阶段:查找和调用依赖关系目标,执行规则)

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

When you use this: echo $$ in your shell, youmay see:当你在 shell 中使用echo $$时,你可能会看到:

echo $$
486

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM