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?


Do you need to simulate an initial log in, you can use sudo -s to just get a root shell.

  • Thanks..seems the man page of sudo suck.. e.g. -s [command] The -s (shell) option runs the shell specified by the SHELL environment variable if it is set or the shell as specified in passwd(5). If a command is specified, it is passed to the shell for execution. Otherwise, an interactive shell is executed. – Howard Mar 7 '12 at 17:35
sudo -i "cd `/bin/pwd` ; /bin/bash"
  • 3
    Fails for me with -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.

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.