7

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?

3 Answers 3

10

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

1
  • 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, 2012 at 17:35
3

-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.

2
sudo -i "cd `/bin/pwd` ; /bin/bash"
1
  • 3
    Fails for me with -bash: cd /path/to/current/dir ; /bin/bash: No such file or directory
    – Sparhawk
    Oct 11, 2014 at 0:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .