Skip to main content
fix a technical explanation
Source Link
mareoraft
  • 280
  • 3
  • 9

There are two ways that it "won't work", depending on your OS:

  1. If your OS follows POSIX, then running sudo cd will cause the external command "cd" (usually located at /usr/bin/cd) to execute in a forked process as the root user. That process changes directories successfully. Once that process is done, you will be returned to your regular shell, which is still in the directory where it started.

So sudo cd runs without error, but does not change the current working directory of your current shell.

(reference: this answer)

  1. Otherwise, running sudo cd will trycause the following to usehappen. Your computer will look through your PATH, trying to find an executable named "cd". It will not find one. (only the shell built-in commandshell built-in command "cd" exists, and that is not an executable file). But sudo cannot run on shell built-in commands Hence, so it willyou get an error.  (On Ubuntu, I get the error message sudo: cd: command not found.)

So sudo cd runs with error.

There are two ways that it "won't work", depending on your OS:

  1. If your OS follows POSIX, then running sudo cd will cause the external command "cd" (usually located at /usr/bin/cd) to execute in a forked process as the root user. That process changes directories successfully. Once that process is done, you will be returned to your regular shell, which is still in the directory where it started.

So sudo cd runs without error, but does not change the current working directory of your current shell.

(reference: this answer)

  1. Otherwise, running sudo cd will try to use the shell built-in command "cd". But sudo cannot run on shell built-in commands, so it will error.  (On Ubuntu, I get the error message sudo: cd: command not found.)

So sudo cd runs with error.

There are two ways that it "won't work", depending on your OS:

  1. If your OS follows POSIX, then running sudo cd will cause the external command "cd" (usually located at /usr/bin/cd) to execute in a forked process as the root user. That process changes directories successfully. Once that process is done, you will be returned to your regular shell, which is still in the directory where it started.

So sudo cd runs without error, but does not change the current working directory of your current shell.

(reference: this answer)

  1. Otherwise, running sudo cd will cause the following to happen. Your computer will look through your PATH, trying to find an executable named "cd". It will not find one. (only the shell built-in command "cd" exists, and that is not an executable file). Hence, you get an error. (On Ubuntu, I get the error message sudo: cd: command not found.)

So sudo cd runs with error.

Source Link
mareoraft
  • 280
  • 3
  • 9

There are two ways that it "won't work", depending on your OS:

  1. If your OS follows POSIX, then running sudo cd will cause the external command "cd" (usually located at /usr/bin/cd) to execute in a forked process as the root user. That process changes directories successfully. Once that process is done, you will be returned to your regular shell, which is still in the directory where it started.

So sudo cd runs without error, but does not change the current working directory of your current shell.

(reference: this answer)

  1. Otherwise, running sudo cd will try to use the shell built-in command "cd". But sudo cannot run on shell built-in commands, so it will error. (On Ubuntu, I get the error message sudo: cd: command not found.)

So sudo cd runs with error.