我想在 C 程序中更改 PATH
变量的值,然后在运行该程序的 shell 中查看更改后的值。
做这样的事情,
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main () {
char *path = getenv ("PATH");
printf ("%s\n\n", path);
setenv ("PATH", strcat (path, ":~/myNewPath/"), 1);
printf ("%s\n\n", path);
int pid = fork ();
if (pid == -1)
abort ();
if (pid == 0) {
} else {
// use execlp? how? source? any hints?
}
return 0;
}
如果我在 exec*
系统调用中使用 source
命令。在 shell 中向后更新此 PATH
变量的语法是什么?
这是不可能的。子进程无法更改其父进程的环境变量。
要了解为什么这是不可能的,请查看 execve
的签名
int execve(const char *program, char *const *argv, char *const *envp);
它与 Unix 系统上main
的true 签名配对
int main(int argc, char **argv, char **envp);
也许您开始明白,就内核而言,环境变量是第二组命令行参数。它们似乎可以通过getenv
和setenv
等独立访问,并且似乎从父子继承,是由 C 库维护的一种错觉。
有关其工作原理的更多详细信息,请研究 the x86-64 ELF ABI specification, section 3.4.1 "Initial Stack and Register State"特别注意图 3.9,它显示了 execve
复制到新创建的堆栈上的数据布局。 (链接的文档特定于一种 CPU 架构,但这种工作方式在现代 Unix 中通常是一致的;细节当然会因 CPU 和 OS 的不同而有所不同。)
我是一名优秀的程序员,十分优秀!