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 exec() family, like execvp()?
Edit: And I just noticed that system("cd") is also meaningless。Thanks for the help of everyone.
execloads an executable file and replaces the current program image with it. As you rightly noted,cdis not an executable file, but rather a shell builtin. So the executable that you want to run is the shell itself. This is of course whatsystem()does for you, but if you want to be explicit about it, you can useexec:Since this replaces your current process image, you should do this after
fork()ing off a new process.However, this entire procedure has absolutely no effect. If you want to change the directory in your current process, use
chdir().