Is there a way to change the environment variables of another process in Unix?

后端 未结 11 2258
别跟我提以往
别跟我提以往 2020-11-22 15:09

On Unix, is there any way that one process can change another\'s environment variables (assuming they\'re all being run by the same user)? A general solution would be best,

11条回答
  •  南方客
    南方客 (楼主)
    2020-11-22 15:38

    It seems that putenv doesn't work now, but setenv does. I was testing the accepted answer while trying to set the variable in the current shell with no success

    $] sudo gdb -p $$
    (gdb) call putenv("TEST=1234")
    $1 = 0
    (gdb) call (char*) getenv("TEST")
    $2 = 0x0
    (gdb) detach
    (gdb) quit
    $] echo "TEST=$TEST"
    TEST=
    

    and the variant how it works:

    $] sudo gdb -p $$
    (gdb) call (int) setenv("TEST", "1234", 1)
    $1 = 0
    (gdb) call (char*) getenv("TEST")
    $2 = 0x55f19ff5edc0 "1234"
    (gdb) detach
    (gdb) quit
    $] echo "TEST=$TEST"
    TEST=1234
    

提交回复
热议问题