Linux:环境变量存储在哪里?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/532155/
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
Linux: where are environment variables stored?
提问by Ben L
If I type into a terminal,
如果我输入终端,
export DISPLAY=:0.0
... where is the shell storing that environment variable?
...存储该环境变量的外壳在哪里?
I'm using Ubuntu 8.10. I've looked in the files ~/.profile and /etc/profile and can find no trace of DISPLAY.
我正在使用 Ubuntu 8.10。我查看了文件 ~/.profile 和 /etc/profile 并且找不到 DISPLAY 的踪迹。
采纳答案by Johannes Schaub - litb
The environment variables of a process exist at runtime, and are not stored in some file or so. They are stored in the process's own memory (that's where they are found to pass on to children). But there is a virtual file in
进程的环境变量存在于运行时,而不是存储在某个文件中。它们存储在进程自己的内存中(这是发现它们传递给子进程的地方)。但是里面有一个虚拟文件
/proc/pid/environ
This file shows all the environment variables that were passed when calling the process (unless the process overwrote that part of its memory — most programs don't). The kernel makes them visible through that virtual file. One can list them. For example to view the variables of process 3940, one can do
这个文件显示了调用进程时传递的所有环境变量(除非进程覆盖了它的那部分内存——大多数程序没有)。内核通过该虚拟文件使它们可见。可以一一列举。比如查看进程3940的变量,可以这样做
cat /proc/3940/environ | tr '##代码##' '\n'
Each variable is delimited by a binary zero from the next one. tr replaces the zero into a newline.
每个变量都由下一个二进制零分隔。tr 将零替换为换行符。
回答by tddmonkey
Type "set" and you will get a list of all the current variables. If you want something to persist put it in ~/.bashrc or ~/.bash_profile (if you're using bash)
键入“set”,您将获得所有当前变量的列表。如果你想保留一些东西,把它放在 ~/.bashrc 或 ~/.bash_profile (如果你使用 bash)
回答by Eduard - Gabriel Munteanu
That variable isn't stored in some script. It's simply set by the X server scripts. You can check the environment variables currently set using set.
该变量未存储在某些脚本中。它只是由 X 服务器脚本设置。您可以使用set检查当前设置的环境变量。
回答by cletus
It's stored in the process (shell) and since you've exported it, any processes that process spawns.
它存储在进程(shell)中,并且由于您已导出它,因此会生成任何进程。
Doing the above doesn't store it anywhere in the filesystem like /etc/profile. You have to put it there explicitly for that to happen.
执行上述操作不会将其存储在文件系统中的任何位置,如 /etc/profile.d 。你必须明确地把它放在那里才能发生。
回答by JohnnyQ
If you want to put the environment for system-wide use you can do so with /etc/environment
file.
如果您想将环境用于系统范围的使用,您可以使用 file.conf 来实现/etc/environment
。