Error: process caused \"exec: \\\"cd\\\": executable file not found in $PATH\"\n"} PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
cd will execute?
cd is a not an executable command on the file system (though POSIX compliant systems have one), it's an internal builtin command of a shell to change its own current working directory.
A process cannot execute a command in a child process to change its own current working directory as the current working directory is a property of each process.
Whatever language you're using to try and execute that
cd command will have its own interface to change the current working directory.
For instance, instead of:
Or whatever command you use to execute a command, in C, use:
chdir() is the libc interface to the
chdir system call.
Note that, if in C, you wrote:
That would not output an error, because that would actually be doing something like:
spawn_cmd("sh", "-c", "cd /some/dir");
that is, run a shell in a child process to evaluate that
cd /some/dir shell code, upon which the shell would invoke its builtin
cd command to change its own current working directory (but again, not the current working directory of the parent process that spawning a process to execute that command).
However, you could do:
system("cd /some/dir && exec some-other-command");
spawn_cmd("sh", "-c", "cd /some/dir && exec some-other-command");
or for arbitrary directory and command to avoid running the risk of them being interpreted as shell code:
spawn_cmd("sh", "-c", "unset -v CDPATH;" "cd -P -- \"$1\" || exit;" "shift;" "exec \"$@\"", "sh", "/some/dir", "some-other-command");
Then, that child process would execute
sh which would change its working directory and then execute that
some-other-command in the same process, so with that new working directory.