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

问题描述:

当在Makefile中插入shell脚本时,我们有(?)使用双美元符号($$)来引用变量.为什么会这样?

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?

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

根据 gnu制作正式文档:

配方中的变量和函数引用具有相同的语法,并且 对makefile中其他位置引用的语义.他们也有 相同的报价规则:如果您希望美元符号出现在您的 食谱,您必须将其加倍("$$").对于像默认外壳这样的外壳, 使用美元符号引入变量,请务必保持 明确您要引用的变量是否为 make变量(使用单个美元符号)或shell变量(使用两个 美元符号).

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变量=>使用两个美元符号