1

This might seem like a duplicate at first sight, but bear with me for a minute.

TLDR; A quick command line way to restore the whole shell environment to the same as upon terminal startup, while not losing the print (scroll up) of the previously executed commands and their outputs.

I would really like a quick way to reset the shell environment to it's starting state, like quitting the the terminal program and starting it again (or opening a new tab). The reason that I don't want to actually quit the program and start it again is that I don't want to lose the history, ie the print of the previous commands and outputs. (Compare to clear in MATLAB, that gets rid of all variables, function definitions et.c. that you've set in that session without removing the print of previous commands and outputs.)

The usual advice people get when asking this is

source ~/.bash_profile

However, for example, this doesn't restore $PATH to the value it has when the terminal program is first opened. I have a few lines like

export PATH=$PATH:/path/to/some/useful/stuff/

in my $PATH and for every time I source, more stuff is added to my $PATH variable. Hence it is not reset to terminal startup value on source. Same thing goes for any other variable set in that session that is not overwritten by some line in the ~/.bash_profile

I've seen people recommend the reset command too. However, that doesn't restore my $PATH to it's original value either. It stays the same as before the reset.

I am just using $PATH as an example here, the question is about resetting the whole shell environment.

A quick and reliable way to reset the shell environment in the sense I describe above would be very handy when experimenting with installations and similar things. Also seeing what the $PATH variable looks like makes me wonder what other things are not reset using source ~/.bash_profile, and it feels unreliable to use as a reset method.

Thank you for reading this far!

Any solutions folks?

2 Answers 2

1

One option for resetting the Terminal session's state except for history is to save the current history to a temp file with something like history -w ~/.history_temp, then close the Terminal window, open a new one, then read back the saved history with history -w ~/.history_temp. Optionally, you can delete the temp history file when you're done.

If you don't want to have to remember & type those commands every time, you can create aliases for them and put those in ~/.bash_profile:

alias savehistory='history -w ~/.history_temp`
alias loadhistory='history -r ~/.history_temp && rm ~/.history_temp`
1
  • Thank you for the reply and solution! Tried upvoting by my rep is too low. I was hoping for something quicker, like a single command to do this. It surprises me that it doesn't seem to be such. It seems like such a standard thing to want to do. I'm most used to scientific computation laguages like R and MATLAB where this is done with a single command (clear for MATLAB). I do understand that there are more stuff involved in the bash env though. I'll wait with the "accept answer" button for a while to see if someone else knows some magic, otherwise I'll accept it. Have a nice evening!
    – Anton
    Commented Apr 11, 2018 at 9:25
1

Setting the original path in a variable at the start of your terminal session would be a simple solution.

ORIG_PATH="$PATH"

Then at anytime you can change the PATH variable back to its original state.

PATH="$ORIG_PATH"

A more complex solution would be to set the PATH to the state set by the loginwindow app then run path_helper as in /private/etc/profile and source ~/.bash_profile.

# Does launchd set the PATH variable?

LPATH=$(launchctl getenv PATH)

if [ -z "$LPATH" ]
then
    PATH=$(getconf PATH)
    eval $(/usr/libexec/path_helper -s)
else
    PATH=$LPATH
    eval $(/usr/libexec/path_helper -s)
fi

. ~/.bash_profile

Both solutions do not require a new terminal session.

1
  • Thank you! Smart solution for $PATH! However, I was hoping for a solution that restores the entire environment in the terminal to the startup state. In my post, $PATH served more of an example showing why solutions suggested elsewhere do not do what I want. The reason that I want to reset the full environment is that I want to be sure that what is working now is going to work tomorrow too and is not dependent on something that is temporarily changed in my environment. l'll clarify my post to emphasize that $PATH is an example. Have a nice day!
    – Anton
    Commented Apr 13, 2018 at 7:17

You must log in to answer this question.

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