37

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?

2 Answers 2

48

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
  • 9
    That wouldn't undo exported environment variables, would it? (Although any that get set by your init file would be reset.)
    – cjm
    Commented Jun 13, 2011 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
    Commented Jun 13, 2011 at 3:42
  • exported variables are kept in bash with exec bash
    – nadapez
    Commented Feb 16 at 15:36
23

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
4
  • 2
    You might need a few others,  like TERM and DISPLAY. Commented Sep 21, 2018 at 3:59
  • 1
    Using env, your previous shell process will still be running. Use exec -l $SHELL if you don't care about the history of previous commands you typed or exec -a $SHELL if you want to keep these in the history of your fresh shell. Most of the time, $SHELL is defined and points at your current shell executable.
    – OpSocket
    Commented Nov 25, 2020 at 17:28
  • "Use exec -l $SHELL if you don't care about the history of previous commands you typed or exec -a $SHELL if you want to keep these in the history of your fresh shell." I'm not sure whether "the history of previous commands you typed" refers to the command history or the command effects. If it refers to the command history, it doesn't work because the history is written/read to/from a file. If it refers to the effects, it depends on the effects, but at least exported variables will persist (unix.stackexchange.com/questions/14885/…).
    – Daniel Le
    Commented Dec 4, 2022 at 1:48
  • Furthermore, exec -a $SHELL is not a valid command, it should be exec -a $SHELL $SHELL. Did I miss anything?
    – Daniel Le
    Commented Dec 4, 2022 at 1:49

You must log in to answer this question.

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