New environment variables missing

Viewed 154

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.

2 Answers

As explained in man 5 proc (pay attention to the bolded paragraph):

/proc/[pid]/environ

This 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 as putenv(3) or modifying the environ(7) variable directly), this file will not reflect those changes.

The variable is set. You can confirm it via:

printf ("%s \n",getenv("TEST"));

which returns 22222 as expected.

Related