Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 8 down vote accepted

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

share|improve this answer
    
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"
share|improve this answer
2  
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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.