cd before sudo
See original GitHub issueWhen using .sudo
within a .cd
context, the script breaks, because the cd
command is passed to sudo
, rather than to the shell, which results in sudo
erroring out with sudo: cd: command not found
Currently working around this issue with c.run("sudo [cmd here]")
, since I have disabled the password prompt.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:11
Top Results From Across the Web
Why won't "sudo cd" work? - Super User
cd is a shell builtin. sudo only works with executables. You could do sudo sh -c 'cd dirname' but as soon as the...
Read more >How to enter a directory with the 'cd' command if it has 700 ...
sudo cd won't work because the cd command is built into the shell. So you are saying become root and then run this...
Read more >why cant I sudo cd into /root [duplicate] - Unix Stack Exchange
when I run sudo cd /root it says sudo: cd: command not found but it works perfectly fine if I first sudo su...
Read more >Why no "sudo cd" - command not found - General Discussion
The whole concept of working directory is a shell-internal thing. Therefore, outside the shell, and in programs like sudo , it has no...
Read more >Get root permissions to cd into a directory without sudo su/-s
The end result on both systems that have cd as a command and Linux is the same.. you end up on the command...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I’ve just been dealing with this issue myself, where I need to run a bunch of
sudo
commands within the same directory. The workaround I’ve used is to wrap the whole sudo command in abash -c
so that it executes in a shell and repeat thecd
for each command:That’s a bit harder on the eyes, but you could potentially write a function to reduce the repetition.
My understanding is that there’s two related issues here:
sudo()
seems to just do simple string concatenation, which isn’t quite enough. For example, if you writesudo('sudo whoami && whoami')
, this translates tosudo whoami && whoami
, of which only the first command has elevated privileges:sudo()
should probably make the whole command run with elevated privileges, more like this:cd
is a shell built-in, rather than a program on the $PATH. So it can’t actually be run with the above simple string concatenation version ofsudo()
. For example:This could also be fixed by wrapping with a shell:
$ sudo bash -c 'cd /tmp'
So, we can’t run commands as root (using sudo) in a different directory than our home directory? That doesn’t really make sense, if I’m being entirely honest.
I do understand that every time a
c.run
orc.sudo
call is made, it is essentially run in a new “session”, resulting it it always being run in a new shell.I also understand that
with c.cd()
modifies the way the command is constructed such, thatcd [dir] &&
is prefixed to the actual command inc.run
.IMO, all this particular fix would entail is making sure that the
cd [dir] &&
prefix goes before thesudo [cmd]
, rather than inside (sudo cd [dir] && cmd
) like it does now.As far as I can tell, there shouldn’t be any reason for
c.cd
andc.sudo
to be incompatible with each other.