When I use these shell commands:

[root@linux /tmp]# a=$$
[root@linux /tmp]# echo $a
3985

where does the value 3985 come from? And why?

link|improve this question

Notice that using $$ for generating temp file names have poor security implications. – hlovdal Aug 15 '11 at 9:00
feedback

2 Answers

up vote 10 down vote accepted
man bash

explains it.

Expands to the process ID of the shell.  In a () subshell, 
it expands to the  process ID of the current shell, not the subshell.
link|improve this answer
feedback

That's why some people use it to construct a filename that's only used temporarily and then destroyed, as in this script fragment.

SCRATCHFILE=/tmp/emphemeral.$$ ;
# Now have the script write to and read from the temp file
rm $SCRATCHFILE ;

As mentioned above, the $$ in the filename will be the PID of the main script.

link|improve this answer
You should use mktemp for this. – Mircea Vutcovici Feb 6 at 20:11
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.