4

In Unix/POSIX/Linux systems it's possible to set the temporary directory via the TMPDIR variable. Wikipedia references a specification by the Open Group mentioning this.

What this doesn't answer for me: Should this contain a trailing slash or not? (I.e. TMPDIR="/foo/bar" or TMPDIR="/foo/bar/").

Based on examples I found online I feel the trailing slash is more common, but I'd prefer a definite answer. Is this standardized / clarified anywhere?

hanno
  • 131
  • 1
  • 2
  • 2
    Been using Linux for over two decades, never had a trailing slash. – Artem S. Tashkinov Feb 02 '22 at 11:38
  • It really doesn't matter, it works either way, but it's cleaner without the trailing slash, since in many cases you'll notice the application adds another slash, so you'll have a double slash. Not a biggie, just less esthetic. As I said, it doesn't really matter. – aviro Feb 02 '22 at 12:35

2 Answers2

3

There is no significant difference between putting a slash at the end and not doing so. With a slash at the end, you'll get paths containing double slashes if they are constructed like $TMPDIR/something, but this should have little or no impact. Only if TMPDIR=/ would this make a difference as double-slash at the start of a pathname may be interpreted especially; see On what systems is //foo/bar different from /foo/bar?.

If $TMPDIR references a directory by naming a symbolic link, that link would not be detectable if the pathname ends with a slash. I.e., if I use TMPDIR=/tmp/dir/ and /tmp/dir is a symbolic link to elsewhere, then [ -L "$TMPDIR" ] would always be false since the symbolic link would be dereferenced.

Some utilities may interpret arguments given with a trailing slash character differently from when they do not have a trailing slash. If that is an issue, then use $TMPDIR/. or ${TMPDIR%/} when you need to ensure that the argument has no trailing slash, and use $TMPDIR/ or ${TMPDIR%/}/ when you need a trailing slash. The substitution ${TMPDIR%/} removes the trailing slash if there is one (but would not remove multiple trailing slashes).

Using a trailing slash or not is not enforced by POSIX. In the end, it comes down to personal preference, but it helps to be consistent. Personally, I don't put slashes at the end of variables that hold pathnames in general.

Also related:

Kusalananda
  • 333,661
2

Adding 10 years to Artem's experience, I fully agree with his comment. Taking care, of course, to avoid using ill written scripts that would blindly concatenate filenames to $TMPDIR

Care should also be taken when using some utilities like rsync that interprets differently a directory name followed by a trailing slash (the files under the directory) and a directory name without trailing slash (the directory itself)

Finally, tmp directories are likely to be symlinked.

And the first point of the trailing slash is to dereference the symlink. In other words, if DIRNAME is a symlink, DIRNAME will refer to the symlink when DIRNAME/ will refer to the directory the symlink points to.

MC68020
  • 7,981