I wonder how in one command I can switch to another user (in my case it is usually root) and still remain in the same location where I am before the change.

I usually do it this way, unfortunately taking many steps:

user1@m:~/loc1/loc2$ pwd

user1@m:~/loc1/loc2$ su -

root@m:~# cd /home/user1/loc1/loc2


I am looking for something like:

user1@m:~/loc1/loc2$ su - && ...

or similar, which will give me this result:

  • Thank you @Christopher! It may be strange, but I did not know that the solution is so simple! Thank you again! – simhumileco Oct 17 '19 at 12:14
  • Great! It'll be the atepted one! :) – simhumileco Oct 17 '19 at 12:19

According the manual, - and -l are the same option.

-l Simulate a full login. The environment is discarded except for HOME, SHELL, PATH, TERM, and USER. HOME and SHELL are modified as above. USER is set to the target login. PATH is set to ``/bin:/usr/bin''. TERM is imported from your current environment. The invoked shell is the target login's, and su will change directory to the target login's home directory.

- (no letter) The same as -l.

By not specifying -l or -, the directory is not changed.

| improve this answer | |

The simplest solution:

user1@m:~/loc1/loc2$ su root

or even (thanks to @Christopher):

user1@m:~/loc1/loc2$ su
| improve this answer | |
  • Now I see how much I don't know yet, thank you for your patience @Christopher! – simhumileco Oct 17 '19 at 12:27

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.