89
23
Why typing sudo cd whatever won't change the directory?
89
23
Why typing sudo cd whatever won't change the directory?
57
cd
is a shell builtin. sudo
only works with executables. You could do sudo sh -c 'cd dirname'
but as soon as the shell exits, you're returned to the directory you started from. If you say what it is you're trying to accomplish then I can help you find a way to do that.
@Arjan That there is an external command cd
in /usr/bin/cd
makes no sense, except the external cd
does something different than the shells cd
. But then, you would have two cd
with different command line syntax. Very odd. The command cd
changes the current directory of the current process. An external command creates a new process, by definition. So it can only change its own, and terminate with no effect to the shell that started it. – Volker Siegel – 2019-10-21T10:25:53.797
1
@Arjan I found the explanation: It is something like a deep compatibility hack. It does not do its function, but it has come of the side effects that the real cd
has. And it can do nothing instead of failing when the current shell does not have a cd
command. Any shell that may ever be used by humans has cd
, including /bin/sh
. But a program that is, seen from the operating system kernel, a shell can be much simpler than that, even only a couple of lines of code. See What is the point of the cd
external command?
2But then sudo pwd
wouldn't work either? (I always figured that sudo cd
does work, but you're just not seeing the result after sudo
returns. But that was just a wild guess. Maybe neither cd
nor pwd
are actually built-in in Bash on a Mac. Running which cd
does indeed give me results. Running sudo cd /
does not give me an error, but indeed does not result in a changed working directory.) – Arjan – 2011-02-03T09:00:05.083
2@Arjan: pwd
is also an external executable so it will work. Note that on some systems, there is a cd
executable, but it's mostly useless. Try using type -a cd
it's much more informative than which
, by the way. – Paused until further notice. – 2011-02-03T09:06:57.797
2Nice! type -a cd
shows both cd is a shell builtin
and cd is /usr/bin/cd
on my Mac. And likewise for pwd
and echo
. And both sudo pwd
and sudo echo "Hello world"
do give me a result. However, type -a return
only yields return is a shell builtin
, and sudo return 3
shows me sudo: return: command not found
. So, I guess the question is: does the OP get an error message, or does the OP not see the cd
work without any error? (Or: what OS is the OP using.) – Arjan – 2011-02-03T09:12:12.717
sudo sh -c 'cd dirname'
doesn't do anything for me. – Peter Niederwieser – 2011-08-09T14:43:09.463
@Peter: See the part of my answer which begins "but..." – Paused until further notice. – 2011-08-11T22:08:49.697
@Dennis: I misunderstood that part. The answer would be easier to understand if cd dirname
was followed up with something (e.g. ; ...
). – Peter Niederwieser – 2011-08-12T00:50:20.947
74
Instead try using sudo -s
to start a root shell and then simply cd
into the directory.
When you're done as root, press CtrlD or type exit
.
As Arjan hints at in his comment below, it is important to note that as root, one can easily do damage to essential system components. Use with care!
4But: be careful, once one is root... – Arjan – 2011-02-03T09:14:02.573
7
You can simply su
to become root and then cd
all you want... I know an answer has already been accepted, but if one is not on the sudoers list then this is the only option.
1On the other hand, if there is no root password, or you don't know it, sudo is the only option. – Liam – 2015-10-20T18:37:49.000
0
There are two ways that it "won't work", depending on your OS:
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)
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.
3
see http://askubuntu.com/questions/291666/why-doesnt-sudo-cd-var-named-work
– John – 2013-09-03T13:08:23.493