<p>I once read that one way to obtain a unique filename in a shell for temp files was to use a double dollar sign (<code>$$</code>).   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.)</p>

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

[{'answer_id': 78501, 'author': 'Flint', 'author_id': 11877, 'author_profile': 'https://Stackoverflow.com/users/11877', 'pm_score': 5, 'selected': False, 'text': '<p>$$ is the id of the current process.</p>\n'}, {'answer_id': 78502, 'author': 'leif', 'author_id': 14257, 'author_profile': 'https://Stackoverflow.com/users/14257', 'pm_score': 2, 'selected': False, 'text': "<p>$$ is the pid of the current shell process.  It isn't a good way to generate unique filenames.</p>\n"}, {'answer_id': 78504, 'author': 'Joe Skora', 'author_id': 14057, 'author_profile': 'https://Stackoverflow.com/users/14057', 'pm_score': 8, 'selected': True, 'text': '<p>In Bash <code>$$</code> is the process ID, as noted in the comments it is not safe to use as a temp filename for a variety of reasons.</p>\n\n<p>For temporary file names, use the <code>mktemp</code> command.</p>\n'}, {'answer_id': 78508, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': "<p>It's the process ID of the bash process.  No concurrent processes will ever have the same PID.</p>\n"}, {'answer_id': 78528, 'author': 'Adam Wright', 'author_id': 1200, 'author_profile': 'https://Stackoverflow.com/users/1200', 'pm_score': 3, 'selected': False, 'text': '<p>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.</p>\n\n<p>This is very much <em>not</em> 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.</p>\n\n<p>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".</p>\n'}, {'answer_id': 78529, 'author': 'Shannon Nelson', 'author_id': 14450, 'author_profile': 'https://Stackoverflow.com/users/14450', 'pm_score': 2, 'selected': False, 'text': '<p>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"</p>\n'}, {'answer_id': 78531, 'author': 'JBB', 'author_id': 12332, 'author_profile': 'https://Stackoverflow.com/users/12332', 'pm_score': 3, 'selected': False, 'text': '<p>$$ 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. </p>\n\n<p>Typically you\'d create something like /tmp/myprogramname$$</p>\n\n<p>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.</p>\n\n<p>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.</p>\n'}, {'answer_id': 78546, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p>$$ 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.</p>\n\n<p>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. </p>\n\n<p>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:</p>\n\n<pre><code>trap "echo \'Cleanup in progress\'; rm -r $TMP_DIR" EXIT\n</code></pre>\n'}, {'answer_id': 78581, 'author': 'emk', 'author_id': 12089, 'author_profile': 'https://Stackoverflow.com/users/12089', 'pm_score': 7, 'selected': False, 'text': '<p><code>$$</code> is the process ID (PID) in bash.  Using <code>$$</code> 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 <a href="http://www.google.com/search?q=tmp+race" rel="noreferrer">these people</a> who created insecure temporary files and had to issue security advisories.</p>\n\n<p>Instead, use <code>mktemp</code>. The <a href="http://linux.die.net/man/1/mktemp" rel="noreferrer">Linux man page for mktemp</a> is excellent. Here\'s some example code from it:</p>\n\n<pre><code>tempfoo=`basename $0`\nTMPFILE=`mktemp -t ${tempfoo}` || exit 1\necho "program output" &gt;&gt; $TMPFILE\n</code></pre>\n'}, {'answer_id': 78667, 'author': 'Kevin Little', 'author_id': 14028, 'author_profile': 'https://Stackoverflow.com/users/14028', 'pm_score': 2, 'selected': False, 'text': '<p>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 <em>reasonably</em> good chance of uniqueness.</p>\n\n<pre><code> -k\n</code></pre>\n'}, {'answer_id': 48744969, 'author': 'Obivan', 'author_id': 5444646, 'author_profile': 'https://Stackoverflow.com/users/5444646', 'pm_score': 0, 'selected': False, 'text': '<blockquote>\n  <p>Also, You can grab login username via this command. Eg.</p>\n</blockquote>\n\n<pre><code>echo $(&lt;/proc/$$/login id). After that, you need to use getent command.\n</code></pre>\n'}, {'answer_id': 64850457, 'author': 'Édouard Lopez', 'author_id': 802365, 'author_profile': 'https://Stackoverflow.com/users/802365', 'pm_score': 1, 'selected': False, 'text': '<p>In <strong>Fish</strong> shell (<code>3.1.2</code>):</p>\n<blockquote>\n<p>The <code>$</code> symbol can also be used multiple times, as a kind of &quot;dereference&quot; operator (the * in C or C++)</p>\n</blockquote>\n<pre><code>set bar bazz\nset foo bar\necho $foo # bar\necho $$foo # same as echo $bar → bazz\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/78493', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/14345/']