-3
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

From where cd will execute?

  • 1
    Please read the How to ask a good question article. Is this for Docker by any chance? There are no clues in your question. – garethTheRed Nov 17 '17 at 7:32
  • This ^^^ and to make it abundantly clear: Make your question full of clues for us to solve it. Otherwise we'll vote to close it. – LinuxSecurityFreak Nov 17 '17 at 7:34
  • Yes, it's a docker container. – user60679 Nov 17 '17 at 10:59
  • @garethTheRed When i run sh -c "cd mydir" got following error: syntax error: unterminated quoted string – user60679 Nov 17 '17 at 11:01
3

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:

spawn_cmd("cd", "/some/dir");

Or whatever command you use to execute a command, in C, use:

chdir("/some/dir")

Where chdir() is the libc interface to the chdir system call.

Note that, if in C, you wrote:

system("cd /some/dir");

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");

same as:

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.

  • This is a docker container, run command I am executing "cd" command and got PATH error, later I tried to run sh -c "cd mydir got syntax error: unterminated quoted string – user60679 Nov 17 '17 at 11:03
  • @user60679, and what are you trying to achieve with that? What's the point of running a sh command in that container that changes its current directory and then exits? If you want to run another command with that new current working directory, run sh -c "cd mydir && exec that-other-command" or perl -e 'chdir("mydir"); exec("that-other-command")', that is you don't have to use a shell to change the cwd, though a shell would be the most obvious command to do that (but the shell's cd has more issues in general than perl's chdir(), hence the CDPATH/-P/-- in my answer). – Stéphane Chazelas Nov 17 '17 at 11:05
  • yes i am running, sh -c " cd mydir && terraform plan" still same error. – user60679 Nov 17 '17 at 12:20
  • @user60679, check the manual of whatever tool you're entering that command line in to check what syntax it recognises. In the end, you want to run the sh command with 3 arguments: sh, -c and cd mydir && terraform plan. It may be that it doesn't recognise " as a quoting operator and you need to enter it as sh -c cd\ mydir&&terraform\ plan or maybe on the contrary that you need to escape those " as in sh -c \"cd mydir && terraform plan\"... – Stéphane Chazelas Nov 17 '17 at 12:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.