Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

New environment variables missing

Tags:

c

linux

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.

like image 845
Mrprimus Avatar asked Mar 01 '26 23:03

Mrprimus


1 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.

like image 157
Arkadiusz Drabczyk Avatar answered Mar 03 '26 11:03

Arkadiusz Drabczyk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!