Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

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

Is there a way to run a command "as if" it is in a new login session?

I've already tried env -i. However, I don't want to deal with various ENV variables I have to set or unset.

I've also tried bash -c "some command" and bash -l -c "some commmand", but they all copy the current environment.

The closest I have come is a ghetto solution: ssh me@localhost "some command"

share|improve this question
Use /bin/bash --login to get that behavior. I use it e.g. to get a proper $PATH. – Daniel Beck Aug 4 '11 at 14:50
That is the equivalent of /bin/bash --l, which I already tried. It copies the original environment. Try it: export SOME_VAL=something. Then /bin/bash --login. Then env | grep SOME_VAL. The value will be there. – dgo.a Aug 4 '11 at 15:03
up vote 9 down vote accepted
su -l $USER

sudo -u $USER -i

For something even more aggressive try env -i bash, but that unsets everything including $HOME and $TERM.

share|improve this answer
Thanks! The second command gives me exactly what I want. I searched for more than 2 hours and did not find anything close to your reply. Based on your reply, I went back to man sudo and found: "if the target user is the same as the invoking user, no password is required." (The su seems to always asks for a password.) I'm such a fool to have overlooked something so simple in the first paragraph of a man page :( I avoided sudo in the first place because I assumed it always asked for a password. Thanks again! – dgo.a Aug 4 '11 at 16:15
just a hint for others... if your logged in as $USER you will need to login as root and then sudo -u ... as your user. If you just do sudo -u as the user you will inherit. – Adam Gent Aug 4 '13 at 21:48

You must log in to answer this question.

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