Using the exec() family to run the “cd” command

后端 未结 5 2035
醉酒成梦
醉酒成梦 2020-12-07 01:30

I know that cd is a shell built-in ,and I can run it by using system().

But is that possible to run the cd command by the

5条回答
  •  伪装坚强ぢ
    2020-12-07 01:52

    No it is not, and it would be of no use. chdir (the function that changes a process's current directory) only affects the process that calls it (and its children). It does not affect its parent in particular.

    So execing cd has no point, since the process would exit immediately after having changed directories.

    (You could exec something like bash -c cd /tmp if you really want to, but as I said, this is fruitless.)

提交回复
热议问题