1

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?

1
  • Notice that using $$ for generating temp file names have poor security implications.
    – hlovdal
    Aug 15, 2011 at 9:00

4 Answers 4

10
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.
3

$$ is the pid of the current process

try

host@controller:~$ echo $$
12481
host@controller:~$ ps -p 12481
  PID TTY          TIME CMD
12481 pts/2    00:00:01 bash
host@controller:~$

Since we execute echo $$ in bash, we get it's current pid

know also

echo $? is the return code of the last executed command.

$# is the number of arguments

$* is the list of arguments passed to the current process

$[1 or 2 or ... n] value of each corresponding argument

0

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.

1
-1

Try "echo $$" you will get the answer.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .