<分区>
程序:
#include<stdio.h>
#include<unistd.h>
int main()
{
char s[100];
printf("%s\n",getcwd(s,100));
chdir("..");
printf("%s\n",getcwd(s,100));
return 0;
}
输出:
$ ./a.out
/home/guest
/home
$
上面的程序改变了一个进程的工作目录。但是,它不会改变当前 shell 的工作目录。因为程序在shell中执行时,shell遵循fork on exec机制。因此,它不会影响当前的 shell。
有没有 有什么方法可以通过这些程序更改 shell 的当前工作目录,例如 shell 使用的内置(cd、echo)命令?