What happened to the TMP environment variable?

☆樱花仙子☆ 提交于 2019-12-03 09:30:34

You are probably thinking of TMPDIR.

This variable shall represent a pathname of a directory made available for programs that need a place to create temporary files.

A good way to create a temporary directory is using mktemp, e.g.

mktemp -d -t

This way, you can even make sure, that your file names won't collide with existing files.

POSIX/FHS says that /tmp is the root for temporary files, although some programs may choose to examine $TEMP or $TMP instead.

Similar to what @Chris Lercher said, I find that this works for me:

dirname $(mktemp -u -t tmp.XXXXXXXXXX)

That won't actually create a temp file (because of the -u flag to mktemp) but it'll give you the directory that temp files would be written to. This snippet works on OSX and Ubuntu (probably other *nix too).

If you want to set it into a variable, do something like this:

TMPDIR=`dirname $(mktemp -u -t tmp.XXXXXXXXXX)`
Global variables command.
# prompt>
printenv | sort
$TMP is not declare(d)
# So, prompt> (i.e. if [ -d /tmp ] is true)
export TMP="/tmp"

export ? In order to pass variables to a subshell.
Also, use $TMPDIR if set, this is the correct var name for Linux. And Yes, I do use the /tmp directory; files will be deleted at least on reboot.!.

FYI - ubuntu (and I assume other systemd based distros) does define the XDG_RUNTIME_DIR variable - which is a per-user temp space, so little more secure than just /tmp :

$ echo $XDG_RUNTIME_DIR /run/user/1000

$ ls -ld $XDG_RUNTIME_DIR drwx------ 2 ubuntu ubuntu 40 Dec 22 15:18 /run/user/1000

I think XDG_RUNTIME_DIR is maintained by systemd/pam, so it won't be set in Dockers or other non-systemd environments.

You can do something like this in ~/.bashrc if you like:

export TEMP="${XDG_RUNTIME_DIR:-/tmp}"

See: https://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html https://www.freedesktop.org/wiki/

Also - there seems to me some caveats with XDG_RUNTIME_DIR and sudo: https://unix.stackexchange.com/questions/346841/why-does-sudo-i-not-set-xdg-runtime-dir-for-the-target-user

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!