I set a new environment variable in my program, but i cannot found it from shell:
int main()
{
if(!setenv("TEST","22222",0)) perror("");
sleep(1000);
return 0;
}
I try it with
$ a.out &
[3] 605
$ Success
$ cat /proc/605/environ|grep 2222
$ ps e 605|grep 2222
$
Any ideas? Many thanks.
As explained in man 5 proc (pay attention to the bolded paragraph):
/proc/[pid]/environThis file contains the initial environment that was set when the currently executing program was started via
execve(2). The entries are separated by null bytes ('\0'), and there may be a null byte at the end. Thus, to print out the environment of process 1, you would do:$ cat /proc/1/environ | tr '\000' '\n'If, after an
execve(2), the process modifies its environment (e.g., by calling functions such asputenv(3) or modifying theenviron(7) variable directly), this file will not reflect those changes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With