I've noticed this before, but it was brought up again as I was answering "How to move directory into a directory with the same name?":
mktemp utility on macOS does not behave the same as the utility of the same name on Linux or BSD (or least OpenBSD) with respect to the
TMPDIR environment variable.
To create a temporary file in the current directory, I can usually say
(and similarly for a temporary directory with
On macOS, I will have to force the utility to use the current directory by giving it an actual template, as in
because using the more convenient
tmpfile=$(TMPDIR=. mktemp) would ignore the
TMPDIR variable and create the file under
/var/folders/qg/s5jp5ffx2p1fxv0hy2l_p3hm0000gn/T or in a similarly named directory.
The manual for
mktemp on macOS mentions that
-t prefixoption is given,
mktempwill generate a template string based on the prefix and the
_CS_DARWIN_USER_TEMP_DIRconfiguration variable if available. Fallback locations if
_CS_DARWIN_USER_TEMP_DIRis not available are
On my system,
_CS_DARWIN_USER_TEMP_DIR appears to be unset:
$ getconf _CS_DARWIN_USER_TEMP_DIR getconf: no such configuration parameter `_CS_DARWIN_USER_TEMP_DIR'
tmpfile=$(TMPDIR=. mktemp -t hello)
still creates a file under
/var/folders/.../ (also when using
$PWD in place of
I'm noticing that
$ getconf DARWIN_USER_TEMP_DIR /var/folders/qg/s5jp5ffx2p1fxv0hy2l_p3hm0000gn/T/
but this doesn't help me much as I wouldn't know how to change this value.
mktemp utility is said to come from FreeBSD, which in turn got it from OpenBSD (which must have been quite a while ago).
Is this a bug (or omission) in the macOS implementation of
mktemp? How do I change the
DARWIN_USER_TEMP_DIR value (or
_CS_DARWIN_USER_TEMP_DIR mentioned by the manual) from within a script (I would ideally want to unset it so that
$TMPDIR takes precedence)?