6

I heard about $TEMP and $TMP, but I think they are not standard for every distro.

As far as I know, the best way to get the temp dir is just /tmp, is there any distro that won't work using that path?

11

$TMPDIR is more standard than both $TEMP and $TMP as it's mentioned by the POSIX standard.

The /tmp directory is retained in POSIX.1-2008 to accommodate historical applications that assume its availability. Implementations are encouraged to provide suitable directory names in the environment variable TMPDIR and applications are encouraged to use the contents of TMPDIR for creating temporary files.

Ref: http://pubs.opengroup.org/onlinepubs/9699919799/xrat/V4_xbd_chap10.html

At least on macOS, $TMPDIR is not set to /tmp by default, but to something like /var/folders/4r/504v61kx02gczk_454db345c0000gn/T/. /tmp is still available though, as a symbolic link to /private/tmp (for whatever reason).

You may use tmpdir="${TMPDIR:-/tmp}" in a script, for example, to use $TMPDIR if it's set, or /tmp if it's not set (or empty).

The non-standard mktemp utility will create a file or directory in $TMPDIR by default and output its name (but not on macOS, see below):

tmpfile=$(mktemp)
tmpdir=$(mktemp -d)

Check the manual for mktemp on your system to figure out how to use it. Not all implementations are the same.


On macOS, because of reasons, you will have to give the mktemp utility a template with an explicit path:

tmpfile=$(mktemp "${TMPDIR:-/tmp}"/tmp.XXXXXXXX)
tmpdir=$(mktemp -d "${TMPDIR:-/tmp}"/tmp.XXXXXXXX)

The above commands would create a temporary file and directory (respectively) in $TMPDIR, or in /tmp if $TMPDIR is empty or if the variable is unset (this variable is by default set to the result of getconf DARWIN_USER_TEMP_DIR on macOS).

  • 3
    @cuonglm What? POSIX says (of its standard utilities) “If applications are written to use temporary or intermediate files, they should use the TMPDIR environment variable, if it is set and represents an accessible directory, to select the location of temporary files.” How is that not standard? – Stephen Kitt Mar 17 '17 at 13:48
  • 1
    @cuonglm that’s a direct copy/paste from the standard. – Stephen Kitt Mar 17 '17 at 13:59
  • 1
    nitpick: ${TMPDIR:-/tmp} would still be expanded to /tmp if $TMPDIR was set but to the empty string. One might argue that an empty $TMPDIR could be regarded as meaning the current directory, but IMO, it's better to consider an empty $TMPDIR as if it was unset and force users to specify the current directory with TMPDIR=., and that avoids having to do things like ${TMPDIR-/tmp}${TMPDIR:+/}file.tmp.$$ – Stéphane Chazelas Mar 17 '17 at 14:07
  • 1
    See also echo 'mkstemp(TEMPLATE)' | m4 -D TEMPLATE="${TMPDIR:-/tmp}/prefixXXXXXX" for a standard way to create a temp file. – Stéphane Chazelas Mar 17 '17 at 14:26
  • 1
    I'm pretty late to this party, yet ${TMPDIR} is empty under Cygwin, while both ${TEMP} and ${TMP} point to /tmp. So my guess is to go with standard, but pay attention to real life — I would go with: ${TMPDIR:-${TEMP:-${TMP:-/tmp}}}. – Cromax Dec 14 '19 at 19:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.