Linux 在 C 中设置环境变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3416638/
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
Set environment variables in C
提问by iman453
Is there a way to set environment variables in Linux using C?
有没有办法在 Linux 中使用 C 设置环境变量?
I tried setenv()
and putenv()
, but they don't seem to be working for me.
我试过setenv()
和putenv()
,但它们似乎对我不起作用。
采纳答案by Carl Norum
I'm going to make a wild guess here, but the normal reason that these functions appear to not work is not because they don't work, but because the user doesn't really understand how environment variables work. For example, if I have this program:
我将在这里做一个疯狂的猜测,但这些函数似乎不起作用的正常原因不是因为它们不起作用,而是因为用户并不真正了解环境变量是如何工作的。例如,如果我有这个程序:
int main(int argc, char **argv)
{
putenv("SomeVariable=SomeValue");
return 0;
}
And then I run it from the shell, it won't modify the shell's environment - there's no way for a child process to do that. That's why the shell commands that modify the environment are builtins, and why you need to source
a script that contains variable settings you want to add to your shell, rather than simply running it.
然后我从 shell 运行它,它不会修改 shell 的环境 - 子进程无法这样做。这就是为什么修改环境的 shell 命令是内置命令的原因,也是为什么您需要source
一个包含要添加到 shell 的变量设置的脚本,而不是简单地运行它。
回答by karlphillip
The environment variable set by setenv()/putenv() will be set for the process that executed these functions and will be inherited by the processes launched by it. However, it will not be broadcastedinto the shell that executed your program.
由 setenv()/putenv() 设置的环境变量将为执行这些函数的进程设置,并由它启动的进程继承。但是,它不会广播到执行您的程序的外壳程序中。
回答by Ben Voigt
The environment block is process-local, and copied to child processes. So if you change variables, the new value only affects your process and child processes spawned after the change. Assuredly it will not change the shell you launched from.
环境块是进程本地的,并复制到子进程。因此,如果您更改变量,新值只会影响您的进程和更改后产生的子进程。肯定不会改变你启动的外壳。
回答by Norman Gray
Any unix program runs in a separate process from the process which starts it; this is a 'child' process.
任何 unix 程序都在与启动它的进程不同的进程中运行;这是一个“子”过程。
When a program is started up -- be that at the command line or any other way -- the system creates a new process which is (more-or-less) a copy of the parent process. That copy includes the environment variables in the parent process, and this is the mechanism by which the child process 'inherits' the environment variables of its parent. (this is all largely what other answers here have said)
当程序启动时——无论是在命令行还是任何其他方式——系统都会创建一个新进程,它是(或多或少)父进程的副本。该副本包括父进程中的环境变量,这是子进程“继承”其父进程的环境变量的机制。(这主要是这里的其他答案所说的)
That is, a process only eversets its ownenvironment variables.
也就是说,这个过程只有不断将其自己的环境变量。
Others have mentioned sourcing a shell script, as a way of setting environment variables in the current process, but if you need to set variables in the current (shell) process programmatically, then there is a slightly indirect way that it's possible.
其他人提到了获取 shell 脚本,作为在当前进程中设置环境变量的一种方式,但是如果您需要以编程方式在当前(shell)进程中设置变量,那么有一种稍微间接的方法是可能的。
Consider this:
考虑一下:
% cat envs.c
#include <stdio.h>
int main(int argc, char**argv)
{
int i;
for (i=1; i<argc; i++) {
printf("ENV%d=%s\n", i, argv[i]);
}
}
% echo $ENV1
% ./envs one two
ENV1=one
ENV2=two
% eval `./envs one two`
% echo $ENV1
one
%
The built-in eval
evaluates its argument as if that argument were typed at the shell prompt. This is a sh-style example; the csh-style variant is left as an exercise!
内置eval
函数评估它的参数,就好像该参数是在 shell 提示符下键入的一样。这是一个 sh 风格的例子;csh 样式的变体留作练习!