22

This issue has been bugging me for a while, and although I've thought I've found my answer through EnvWatcher, unfortunately it only works on Bash. And I use zsh.

I would like to replicate the things env-watcher does, to a lesser degree maybe, but I need to know if there is a command by which I could reset a shell to it's clean startup stage.

As for example, I'd like to have the following workflow.

source some-functions
alias another-thing
export SVN_EDITOR=vim
RESET-ZSH # none of the above are valid any more

Is there such a built in possibility, or am I chasing butterflies?

29

You could just exec zsh, which will give you a fresh zsh and re-run the init functions. Note that you'd need to exec zsh -l for a login zsh to keep its "login shell" status. I don't know how well it preserves command history (it seems to work for me, but if you use multiple shells in different terminals you might get 'crosstalk' between the two shells' history)

  • 3
    That wouldn't undo exported environment variables, would it? (Although any that get set by your init file would be reset.) – cjm Jun 13 '11 at 3:40
  • Probably it wouldn't. I hadn't considered that aspect because I wasn't thinking of environment variables as something you set temporarily in the shell. – Random832 Jun 13 '11 at 3:42
15

To undo exported environment variables, you can use the env command env -i zsh. env runs the command given as an argument, and -i gives it a clean environment.

Depending on your configuration, you may need to append -l to zsh / bash in order to load your .profile, and you also may need to "bootstrap" your environment with $HOME so that the shell knows where to find your startup files. Altogether, this looks like:

env -i HOME="$HOME" zsh -l
  • 1
    You might need a few others,  like TERM and DISPLAY. – G-Man Sep 21 '18 at 3:59

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.