When use sudo -i
I will simulate login as root, and current working directory will be jumped to /root.
Is it possible not to jump and keep the working directory unchanged?
Server Fault is a question and answer site for system and network administrators. It only takes a minute to sign up.
Sign up to join this communityWhen use sudo -i
I will simulate login as root, and current working directory will be jumped to /root.
Is it possible not to jump and keep the working directory unchanged?
Do you need to simulate an initial log in, you can use sudo -s
to just get a root shell.
sudo -i "cd `/bin/pwd` ; /bin/bash"
-bash: cd /path/to/current/dir ; /bin/bash: No such file or directory
– Sparhawk
Oct 11 '14 at 0:06
-s option does not allow me to run commands which can be found only after user login (PATH is updated in the .bash_profile)
Answer 'Joel K' is cause of error. So correct way is:
sudo -i -- bash -c "cd '$PWD'; echo"
Use your own command instead of echo.