Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. Why is that so?

for number in 1 2 3 4 ; do \     echo $$number ; \ done 
like image 610
JohnTortugo Avatar asked Oct 25 '14 16:10

JohnTortugo


People also ask

What is $$ in Makefile?

$$ means be interpreted as a $ by the shell. the $(UNZIP_PATH) gets expanded by make before being interpreted by the shell.

What does dollar sign do in Makefile?

$@ (“dollar at”) is part of the Makefile language. In your recipe, it refers to the thing it is going to build (above, that's prog ). Mnemonic: it's the target you're aiming at. $^ (“dollar caret”) refers to the dependencies/inputs (above, that's main. c ).

What does $$ mean in shell script?

$$ is the pid (process id) of the shell interpreter running your script. It's different for each process running on a system at the moment, but over time the pid wraps around, and after you exit there will be another process with same pid eventually.As long as you're running, the pid is unique to you.

What does $$ mean Linux?

$$ means the process ID of the currently-running process.


1 Answers

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
like image 195
anubhava Avatar answered Oct 12 '22 15:10

anubhava