CHDIR()不影响环境变量PWD [英] chdir() not affecting environment variable PWD

查看:193
本文介绍了CHDIR()不影响环境变量PWD的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用CHDIR()来改变当前的工作目录下,为什么不GETENV(PWD)给予present工作目录?我需要SETENV(PWD,NEWDIR,1)也?

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?

void intChangeDir(char *newDir)
{
    if( chdir(newDir)==0 )              
    {
        printf("Directory changed. The present working directory is \"%s\" \"%s\"\n",getenv("PWD"),getcwd(NULL,0));
    }
    else
    {
        printf("Error changing dir %s\n",strerror(errno));      
    }
}

输出:(可执行文件的位置是/ home /用户)

Output: (location of the executable is /home/user)

changedir的/ boot

changedir /boot

目录改变。在present工作目录是/ home / user中/启动

Directory changed. The present working directory is "/home/user" "/boot"

推荐答案

是的,如果你想改变环境变量,你必须明确地做到这一点。

Yes, if you want to change the environment variable, you have to explicitly do that.

这是外壳,设置并在事件的正常运行更新 PWD ,所以它只反映已知的外壳当前目录的变化。

It's shell that sets and updates PWD in the normal run of events, so it only reflects changes of the current directory known to the shell.

这篇关于CHDIR()不影响环境变量PWD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆