I'm looking to figure something out in an existing script I'm trying to understand.
the command in question(on a Solaris Box using KSH) is: WORKDIR=/tmp/namereplaced.exec.$$.$RANDOM
Now, I know it's setting the $workdir environmental variable...
And I understand most of the command.
What I don't get is what the ".$$." is doing?
When I run the command and then echo $WORKDIR, I get:
/tmp/namereplaced.12954.15085
On repeatedly running the command and echoing the variable, the second number (15085) changes each time as it's randomly generated.
But the first number (12954 above) remains the same. I am assuming that number comes from the ".$$." segment of the command, but I do not understand how or why?
$$ is a special variable meaning "the process ID of the current shell". If you quit and logged back in, you'd get a new one. It's not impossible for the same process ID to be repeated eventually, but two processes will never have the same process ID at the same time.
Sometimes you see it used in naming temporary files. Instead of /tmp/filename, you'd do /tmp/$$-filename, so that if someone manages to run two instances of your shell script simultaneously, they won't stomp over each other's tempfiles. Often you see mktemp used instead, which just generates a long, unique filename with no relation to your process id, since that makes it harder for outsiders to guess what process is using what file.
$$.$RANDOM Seems redundant to me, I'd have used one or the other, but I guess they were feeling paranoid that day. Or had requirements I'm not aware of yet.