bash TMP 环境变量发生了什么变化?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2435062/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What happened to the TMP environment variable?
提问by boatcoder
I always heard that the proper way to find the temporary folder on a UNIX machine was to look at the TMP environment variable. When writing code that worked on Windows as well as Linux, I would check for TEMP and TMP.
我一直听说在 UNIX 机器上找到临时文件夹的正确方法是查看 TMP 环境变量。在编写适用于 Windows 和 Linux 的代码时,我会检查 TEMP 和 TMP。
Today, I discovered that my Ubuntu install does not have that environment variable at all.
今天,我发现我的 Ubuntu 安装根本没有那个环境变量。
I know it seems you can always count on /tmp being there to put your temporary files in, but I understood that TMP was the way the user could tell you to put the temporary files someplace else.
我知道似乎您总是可以指望 /tmp 将临时文件放在那里,但我知道 TMP 是用户可以告诉您将临时文件放在其他地方的方式。
Is that still the case?
还是这样吗?
采纳答案by Alok Singhal
回答by Chris Lercher
A good way to create a temporary directory is using mktemp, e.g.
创建临时目录的一个好方法是使用 mktemp,例如
mktemp -d -t
This way, you can even make sure, that your file names won't collide with existing files.
这样,您甚至可以确保您的文件名不会与现有文件冲突。
回答by Ignacio Vazquez-Abrams
POSIX/FHS says that /tmp
is the root for temporary files, although some programs may choose to examine $TEMP
or $TMP
instead.
POSIX/FHS 说这/tmp
是临时文件的根,尽管有些程序可能会选择检查$TEMP
或$TMP
代替。
回答by Tom Saleeba
Similar to what @Chris Lercher said, I find that this works for me:
与@Chris Lercher 所说的类似,我发现这对我有用:
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).
这实际上不会创建临时文件(因为 mktemp 的 -u 标志),但它会给你临时文件将被写入的目录。此代码段适用于 OSX 和 Ubuntu(也可能是其他 *nix)。
If you want to set it into a variable, do something like this:
如果要将其设置为变量,请执行以下操作:
TMPDIR=`dirname $(mktemp -u -t tmp.XXXXXXXXXX)`
回答by JimmyLandStudios
# Let's look at environment variable's
printenv | sort
# search for TMP var
printenv | grep TMP
$TMP 未声明,因为它是 $TMPDIR$TMPDIR[Answer回答]
## [ -d /tmp ] && echo 'is true'
export TMP='/tmp' # In order to pass variables to a subshell.
Use $TMPDIR , this is the correct var name for Linux.Notice: the /tmp directory contents (files) will be deleted on Shutdown/REBOOT, This should be expected/desired.
使用 $TMPDIR ,这是 Linux 的正确 var 名称。注意: /tmp 目录内容(文件)将在关机/重启时被删除,这应该是预期的/期望的。
回答by Reuben
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 :
仅供参考 - ubuntu(我假设其他基于 systemd 的发行版)确实定义了 XDG_RUNTIME_DIR 变量 - 这是一个每个用户的临时空间,所以比 /tmp 更安全:
$ echo $XDG_RUNTIME_DIR /run/user/1000
$ echo $XDG_RUNTIME_DIR /run/user/1000
$ ls -ld $XDG_RUNTIME_DIR drwx------ 2 ubuntu ubuntu 40 Dec 22 15:18 /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.
我认为 XDG_RUNTIME_DIR 是由 systemd/pam 维护的,因此不会在 Docker 或其他非 systemd 环境中设置。
You can do something like this in ~/.bashrc if you like:
如果你愿意,你可以在 ~/.bashrc 中做这样的事情:
export TEMP="${XDG_RUNTIME_DIR:-/tmp}"
导出 TEMP="${XDG_RUNTIME_DIR:-/tmp}"
See: https://standards.freedesktop.org/basedir-spec/basedir-spec-latest.htmlhttps://www.freedesktop.org/wiki/
请参阅: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
另外 - 在我看来,XDG_RUNTIME_DIR 和 sudo 有一些警告:https://unix.stackexchange.com/questions/346841/why-does-sudo-i-not-set-xdg-runtime-dir-for-the-target -用户