What does $$ mean in the shell?
Asked Answered
A

12

161

I once read that one way to obtain a unique filename in a shell for temp files was to use a double dollar sign ($$). This does produce a number that varies from time to time... but if you call it repeatedly, it returns the same number. (The solution is to just use the time.)

I am curious to know what $$ actually is, and why it would be suggested as a way to generate unique filenames.

Aspiration answered 16/9, 2008 at 23:59 Comment(0)
I
113

In Bash $$ is the process ID, as noted in the comments it is not safe to use as a temp filename for a variety of reasons.

For temporary file names, use the mktemp command.

Intoxicative answered 17/9, 2008 at 0:2 Comment(6)
For people who just look at the top answer, $$ is not fine for even a single file if writing to a publicly-writable directory (e.g., /tmp). It's easy to litter /tmp with symlinks that will cause your script to write somewhere undesirable. mktemp is much better.Detrusion
Yeah, using $$ will cause a nasty security hole. Don't do it.Deport
Or the person who asked the question should set the higher rated answer below as the accepted answer...Excreta
I add "dollar dollar" for SEO.Landaulet
If $$ just outputs the pid, how can it be "not safe" ? Security hole is created by virtue of poor design not use of PID in tmp filename... If you care about integrity of the output that goes to tmp so much, maybe you shouldn't even put it there in the first place? $$ will not cause security hole, lack of foresight willCommensurate
funny that git seems to use $$ for merge tempfilesBifarious
D
127

$$ is the process ID (PID) in bash. Using $$ is a bad idea, because it will usually create a race condition, and allow your shell-script to be subverted by an attacker. See, for example, all these people who created insecure temporary files and had to issue security advisories.

Instead, use mktemp. The Linux man page for mktemp is excellent. Here's some example code from it:

tempfoo=`basename $0`
TMPFILE=`mktemp -t ${tempfoo}` || exit 1
echo "program output" >> $TMPFILE
Deport answered 17/9, 2008 at 0:13 Comment(2)
Thanks. The mktemp option -t is now deprecated (I think because of problems with the char -). Use mktemp ${tempfoo}.XXXXXX these days. I take the liberty to update your post.Paramaribo
Please also note, that the backtick was deprecated, so use TMPFILE=$(mktemp) instead.Cyprinodont
I
113

In Bash $$ is the process ID, as noted in the comments it is not safe to use as a temp filename for a variety of reasons.

For temporary file names, use the mktemp command.

Intoxicative answered 17/9, 2008 at 0:2 Comment(6)
For people who just look at the top answer, $$ is not fine for even a single file if writing to a publicly-writable directory (e.g., /tmp). It's easy to litter /tmp with symlinks that will cause your script to write somewhere undesirable. mktemp is much better.Detrusion
Yeah, using $$ will cause a nasty security hole. Don't do it.Deport
Or the person who asked the question should set the higher rated answer below as the accepted answer...Excreta
I add "dollar dollar" for SEO.Landaulet
If $$ just outputs the pid, how can it be "not safe" ? Security hole is created by virtue of poor design not use of PID in tmp filename... If you care about integrity of the output that goes to tmp so much, maybe you shouldn't even put it there in the first place? $$ will not cause security hole, lack of foresight willCommensurate
funny that git seems to use $$ for merge tempfilesBifarious
E
24

$$ is the id of the current process.

Emergency answered 17/9, 2008 at 0:1 Comment(0)
L
8

Every process in a UNIX like operating system has a (temporarily) unique identifier, the PID. No two processes running at the same time can have the same PID, and $$ refers to the PID of the bash instance running the script.

This is very much not a unique idenifier in the sense that it will never be reused (indeed, PIDs are reused constantly). What it does give you is a number such that, if another person runs your script, they will get a different identifier whilst yours is still running. Once yours dies, the PID may be recycled and someone else might run your script, get the same PID, and so get the same filename.

As such, it is only really sane to say "$$ gives a filename such that if someone else runs the same script whist my instance is still running, they will get a different name".

Latvian answered 17/9, 2008 at 0:5 Comment(0)
A
7

$$ is your PID. It doesn't really generate a unique filename, unless you are careful and no one else does it exactly the same way.

Typically you'd create something like /tmp/myprogramname$$

There're so many ways to break this, and if you're writing to locations other folks can write to it's not too difficult on many OSes to predict what PID you're going to have and screw around -- imagine you're running as root and I create /tmp/yourprogname13395 as a symlink pointing to /etc/passwd -- and you write into it.

This is a bad thing to be doing in a shell script. If you're going to use a temporary file for something, you ought to be using a better language which will at least let you add the "exclusive" flag for opening (creating) the file. Then you can be sure you're not clobbering something else.

Anastase answered 17/9, 2008 at 0:6 Comment(0)
O
4

$$ 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.

From the definition above it should be obvious that no matter how many times you use $$ in a script, it will return the same number.

You can use, e.g. /tmp/myscript.scratch.$$ as your temp file for things that need not be extremely reliable or secure. It's a good practice to delete such temp files at the end of your script, using, for example, trap command:

trap "echo 'Cleanup in progress'; rm -r $TMP_DIR" EXIT
Omega answered 17/9, 2008 at 0:8 Comment(0)
G
3

$$ is the pid of the current shell process. It isn't a good way to generate unique filenames.

Grouch answered 17/9, 2008 at 0:1 Comment(0)
T
2

It's the process ID of the bash process. No concurrent processes will ever have the same PID.

Taboo answered 17/9, 2008 at 0:2 Comment(0)
L
2

The $$ is the process id of the shell in which your script is running. For more details, see the man page for sh or bash. The man pages can be found be either using a command line "man sh", or by searching the web for "shell manpage"

Lennielenno answered 17/9, 2008 at 0:5 Comment(0)
C
2

Let me second emk's answer -- don't use $$ by itself as a "unique" anything. For files, use mktemp. For other IDs within the same bash script, use "$$$(date +%s%N)" for a reasonably good chance of uniqueness.

 -k
Citizenry answered 17/9, 2008 at 0:29 Comment(0)
R
1

In Fish shell (3.1.2):

The $ symbol can also be used multiple times, as a kind of "dereference" operator (the * in C or C++)

set bar bazz
set foo bar
echo $foo # bar
echo $$foo # same as echo $bar → bazz
Rostrum answered 15/11, 2020 at 22:29 Comment(0)
S
0

Also, You can grab login username via this command. Eg.

echo $(</proc/$$/login id). After that, you need to use getent command.
Selfmoving answered 12/2, 2018 at 11:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.