chdir() not affecting environment variable PWD

后端 未结 3 1228
醉梦人生
醉梦人生 2020-12-20 19:59

When I use chdir() to change the current working directory, why doesn\'t the getenv(\"PWD\") give the present working directory? Do I need to setenv(\"PWD\",newDir,1) also?<

3条回答
  •  心在旅途
    2020-12-20 20:46

    "getenv" gets PWD from the environment that the program started from. "PWD" equalling the current working directory is something maintained by the shell, and since you changed directory in the program you started from the shell rather than the shell, PWD hasn't changed in the environment.

    You'll probably also notice that when your program ends, the shell is still at the directory you started at. The shell hasn't changed directory, so PWD hasn't changed.

提交回复
热议问题