C程序中的chdir系统调用

标签 c linux bash shell unix

<分区>

程序:

    #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)命令?

最佳答案

Is there any way to change the current working directory of the shell via these program like buildin(cd,echo) command used by the shell.

你不能那样做。

允许子进程更改父进程的当前目录或与此相关的任何状态会对父进程造成严重破坏。

关于C程序中的chdir系统调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33139865/

相关文章:

c - 如何在 C 语言的 Linux POSIX 模型中共享现有的动态数组?

c - 为单链表编写适当的push() 函数。

linux - 如何检查程序终止后使用了多少内存和时间?

linux - 在下一个 Debian 小版本中库版本会变得不可用吗?

"if a file exists, read it into variable, else empty string"的 Bash 单行代码?

c - strtok 中的段错误(核心已转储)

Linux 内核构建编译错误 stringop-overflow 和 sizeof-pointer-memaccess

mysql - Bash:即使条件为真也无法进入

linux - 带有参数的 shell 脚本

c - 为什么增加进程数不会减少此并行代码的执行时间?