What is the meaning of a double dollar sign in bash/Makefile?
LinuxBashMakefileLinux Problem Overview
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
Linux Solutions
Solution 1 - Linux
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).
So in short:
-
makefile variable => use a single dollar sign
-
shell variable => use two dollar signs
Solution 2 - Linux
Not directly applicable to this example -- except if the code shown is executed via $(shell ...)
instead of being a rule:
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)
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.
Solution 3 - Linux
$$ is the PID of your current process.
$ 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