Most of the scripts that I need to write don’t actually need to manipulate a lot of data. It is usually enough to pipe that data through a command or two and use the result. When this isn’t acceptable then it is usually a small enough amount of data that I can save it in a temporary file with the same name.
Yet there might be a number of cases where truly unique names are necessary. One example might be if the script gets run many times in parallel but still needs to store some temporary data.
There are a few different methods of generating a unique name.
Each Unix process has a unique id and that can be used as part of the file name. There are some special variables that can be accessed and one of them is $$. This bash shell variable will be the actual process id of the current shell.
This actually should be pretty safe for a lot of situations depending on the volume.
Another fairly classic method of creating a unique name would be to create a timestamp. This is a terrible device for coming up with a unambiguous name unless the script in question is being run in serial.
This example simply uses the date command to display the current date and time. This date and time is redirected using the backtick. This can also be done using the more modern method of redirecting the output.
Operating system support
There exists the mktemp command which will create an absolutely unique name based on the names of the files or directories in the destination directory. There is one “little” side effect of this command and that is that the temporary file or directory is created. The file is a zero length file while the directory is a normal directory.
Using the same technique as in the previous example it is possible to both create a unique name while also capturing the name in a variable.
TEMPFILE=$(mktemp /tmp/mytempdata.XXXX) TEMPDIR=$(mktemp -d /tmp/mytempdata.XXXX)
The operating system cannot know how many free files you might need in your directory. The mktemp command will replace the capital letter X with the unique alphanumeric value. If more than 999 files might be in use at a time then use four X’s. The exact location of the capital X is not important. It can be set as an extension or just a value within the name.