过程的当前值'环境变量 [英] Current value of process' environment variable

查看:158
本文介绍了过程的当前值'环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有从bash进程设置环境变量,并从另一个阅读的一种方式。

I was wondering if there is a way to set an environment variable from a bash process and read it from another.

由于环境变量的值是本地进程(除了继承),人们可以不只是做出口FOO =酒吧在终端和阅读另一个。
当时我想通过的/ proc / ENVIRON 来得到他们,但是这是我得到:

As environment variables' values are local to processes (besides the inheritance), one can't just do export FOO="bar" in a terminal and read it from another. I was then trying to get them through /proc/environ, but this is what I got:

etuardu@subranu:~$ FOO="foo" bash
etuardu@subranu:~$ strings /proc/$$/environ | grep FOO
FOO=foo
etuardu@subranu:~$ export FOO="bar"
etuardu@subranu:~$ strings /proc/$$/environ | grep FOO
FOO=foo
etuardu@subranu:~$ echo $FOO
bar

看来我刚刚得到的值,即环境变量已经当这个过程开始了。结果
怎么样当前值?

It seems I can just get the value that that environment variable had when the process started.
How about its current value?

推荐答案

这是一般不可能的,因为这样的环境变量工作。

This is not possible in general because of the way environment variables work.

当一个进程首先是 EXEC 版,其初始设置环境变量有一些其他的东西(统称为内核耗材主要是它的的argv 载体,即其命令行)。此后,这个列表(如的argv 矢量)只是过程里面字符指针的常规的C数组。这个过程是自由地管理它们是什么样子,包括完全回收保存字符串用于其他用途,如果要记忆。它是不安全的一个进程去任何其他进程的内存空间偷看里面找到它的环境变量。

When a process is first execed, the kernel supplies it with its initial set of environment variables together with some other stuff (mainly its argv vector, i.e. its command line). Thereafter, this list (like the argv vector) is just a regular C array of character pointers inside the process. The process is free to manage them however it likes, including completely recycling the memory that holds the strings for some other use if it wants to. It isn't safe for one process to go peeking inside any other process's memory space to find its environment variables.

大多数类型的进程确实使用内核或多或少,或许是查询它与C库函数如的getenv()在修改 putenv()函数。如果这些过程运行反过来其任何其他可执行文件,它们经过相同的环境矢量它们在他们自己的执行开始接收的 execve的系统调用,这意味着该新的可执行文件得到相同的环境(可能由一些调用增强 putenv()函数)。

Most types of processes do use the list of environment variables supplied by the kernel more or less as-is, perhaps querying it and modifying it with C library functions like getenv() and putenv(). If these processes run any other executables in their turn, they pass the same environment vector to the execve system call which they received at the start of their own execution, which means that the new executable gets the same environment (possibly enhanced by some calls to putenv()).

壳是另一回事。由于环境变量是在shell脚本中如此重要,一些炮弹使用提供给他们的环境载体只能作为初始值,并随后将其忽略。他们用自己的,能力更强,数据结构,管理他们的环境变量。当他们执行自己的子进程,他们通过一个全新的环境载体,新工艺,从这些内部数据结构构成。这意味着,即使你要窥视到一个shell的内存空间来找到它的环境变量按照以上建议,你会发现只有初始设定,而不是外壳实际使用环境!

Shells are another matter. Because environment variables are so important in shell scripts, some shells use the environment vector supplied to them only as a "start value", and thereafter ignore it. They manage their environment variables using their own, more capable, data structures. When they execute child processes of their own, they pass a completely new environment vector to the new process, constructed from those internal data structures. This means that even if you were to peek into a shell's memory space to find its environment variables as suggested above, you would find only the initial set, not the environment that the shell is actually using!

您的可能是什么的能够做的是查询过程的初始设置环境变量,也就是传递给可执行文件,当它开始相同的向量。但是,这是不可移植的,甚至还有中支持此操作系统相当大的变化。例如,在历史UNIX系统,它仍然是有点难看,因为它仍然涉及偷看到进程的存储器空间(虽然被称为用户区的存储器中的特殊区域)。 Linux的使这成为可能更优雅:向量可以发现在文本字符串的/ proc /< PID> / ENVIRON 。在大多数系统中有可能得到这个信息, PS 有一个电子选项,它能够证明这一点。

What you might be able to do is query a process's initial set of environment variables, that is, the same vector that was passed to the executable when it started. But this is not portable, and there is considerable variation even among operating systems that support this. For example, in historical UNIXes, it is still a bit ugly because it still involves peeking into the process's memory space (albeit a special area of memory called the "user area"). Linux makes this possible more elegantly: the vector can be found as a text string in /proc/<pid>/environ. On most systems where it's possible to get this information, ps has an e option which is able to show it.

这篇关于过程的当前值'环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆