When I login to root via

sudo -i

sudo changes the working directory of new shell to root's home directory. I can use following commands to keep working directory of parent shell:

sudo -s
sudo su

but none of these invoke login shell, which means that files like /etc/profile.d/foobar.sh are NOT sourced.

How can you you invoke login shell via sudo and stay at the same directory?


I can use

sudo zsh -l

The downside is that I'm hard-coding the shell, but that's no big deal, since it probably won't change in my lifetime. :-)

  • Have you tried su--preserve-environment? Edit: on mobile, can't test it. I reckon that you could just run sudo (shell) `pwd`" where (shell)` is your shell – Stan Strum May 17 '18 at 18:29
  • @StanStrum I want to use sudo, because I don't want to type root's password. – Mark Howard May 17 '18 at 18:31
  • @MarkHoward And the 2nd suggestion? – Stan Strum May 17 '18 at 18:31
  • @StanStrum that's what's in my EDIT. If you append -l it works. – Mark Howard May 17 '18 at 18:32
  • You could use getent to look up root's shell, but that might be overkill. – Ulrich Schwarz May 17 '18 at 20:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.