Restart bash from terminal without restarting the terminal application (mac)?

I’ve looked around and bit for an answer to this question but I don’t seem to find it (which is weird).

My question is, is there any simple way to restart the BASH session from within the terminal on Mac. I just want the same behaviour as if I closed the terminal application and started it again (all variables reset, .bash_profile sourced etc).

I know how to source .bash_profile, but that’s not what I want. One of the reasons I want to do this is because a plugin for my BASH prompt has code that prevents colors from being loaded multiple times. Therefore, sourcing .bash_profile doesn’t reload the color variables and I have to restart the terminal application to get changes in effect.

Asked By: Mattias

||

exec bash should replace the current shell process with (a new instance of) bash.

EDIT: Seems from answers below that Catalina replaces bash with zsh. You can run exec zsh in that case, or alternatively exec "$SHELL" which should always use your default shell.

Answered By: Muzer

tl;dr use exec $SHELL if you want the start the same shell you’re in.
The accepted answer will only work for you if you’re using bash.

The accepted answer is great for a shell that’s using bash, which is also what the OP asked, however, I’m using Terminal on macOS with a non-default shell (zsh) and running the command exec bash obviously doesn’t have the desired effect: it launches bash instead of my default shell.

So the environment-agnostic command would be exec $SHELL which utilizes the environment variable $SHELL that stores the default shell for the user.

Also, note that Apple replaced bash with zsh in macOS Catalina so to have the effect the OP is looking for running either exec zsh or exec $SHELL is the way to go after updating to macOS 10.15.

Answered By: phip

According to this article and others your shell doesn’t change when you upgrade to Catalina. But any new accounts will be zsh. You can change those to bash if desired. bash will not be part of macOS at some time in the future. Apparently the licensing for zsh is less restrictive.

I came upon this article after getting tired of quitting and restarting iTerm for the same reason as the OP knowing there was a command such as exec zsh.

Answered By: Greg

The shortest variant:

$ exec $0
Answered By: user2240578

This worked for me:

exec env -i HOME=$HOME bash -l
  • exec replaces the current shell
  • env -i HOME=$HOME resets all existing env vars and sets HOME for the new shell to the old $HOME
  • bash -l starts a new login shell (zsh probably has a similar command)

I added this as an alias and it works fine:

alias reload='echo "Reloading shell...";exec env -i HOME=$HOME bash -l'
Answered By: rvirk
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.