A slightly more portable way to handle temporary files is to use
mktemp. It'll create temporary files and return their paths for you. For instance:
$ ls /tmp/tmp.zVNygt4o7P
You could use it in a script quite easily:
echo "Some temp. data..." > $tmpfile
Reading the man page, you should be able to set options according to your needs. For instance:
-d creates a directory instead of a file.
-u generates a name, but does not create anything.
-u you could retrieve the temporary directory quite easily with...
$ tmpdir=$(dirname $(mktemp -u))
More information about
mktemp is available here.
Edit regarding Mac OS X: I have never used a Mac OSX system, but according to a comment by Tyilo below, it seems like Mac OSX's
mktemp requires you to provide a template (which is an optional argument on Linux). Quoting:
The template may be any file name with some number of "Xs" appended to it, for example
/tmp/temp.XXXX. The trailing "Xs" are replaced with the current process number and/or a unique letter combination. The number of unique file names mktemp can return depends on the number of "Xs" provided; six "Xs" will result in mktemp selecting 1 of 56800235584 (62 ** 6) possible file names.
The man page also says that this implementation is inspired by the OpenBSD man page for
mktemp. A similar divergence might therefore be observed by OpenBSD and FreeBSD users as well (see the History section).
Now, as you probably noticed, this requires you to specify a complete file path, including the temporary directory you are looking for in your question. This little problem can be handled using the
-t switch. While this option seems to require an argument (
prefix), it would appear that
mktemp relies on
$TMPDIR when necessary.
All in all, you should be able to get the same result as above using...
$ tmpdir=$(dirname $(mktemp tmp.XXXXXXXXXX -ut))
Any feedback from Mac OS X users would be greatly appreciated, as I am unable to test this solution myself.