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.

up vote 43 down vote accepted

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

  • 1
    Is bash in the terminal application an interactive shell, and does exec bash only restart that interactive shell then? Also, can you explain exactly what the exec command does and it's options? Thanks! – Mattias Jul 23 '15 at 15:38
  • exec replaces the current process with a new one. Bash has a very well-written but unfortunately hard to search manpage; type man bash, find the last instance of the string SHELL BUILTIN COMMANDS and then scroll down to exec. In short, passing the -c flag will run it without any environment variables (which might be required for your purposes, I don't know), and the other arguments -l and -a are probably not things you want (they seem to be historical/obscure and specific features). You can also use exec for opening new file descriptors in the current shell, if you leave off the command. – Muzer Jul 23 '15 at 15:44
  • Can you show an example of how to use exec for opening new file descriptors in the current shell? And what about my question about interactive/login shell. Is the shell run in the terminal an interactive shell, and is that all the exec command touches. Lastly, this question's approved answer recommend the exec command with the -l flag, but I don't understand what it does. Sorry for asking so much, I just wanna have an understanding of the commands I use. – Mattias Jul 23 '15 at 15:54
  • 1
    Sorry, yes. I'm not sure about Macs, but in the Linux world at least shells run from a graphical terminal are (usually) not login shells, and you are only restarting the interactive shell and not the terminal itself or your login shell. -l here is an argument to bash and just makes it behave like a login shell. Opening file descriptors in the current shell has nothing to do with your question, but you can use exec 4<>test.txt for example to make file descriptor 4 point to test.txt. Then in the rest of your shell you can do echo test>&4 to output to test.txt. – Muzer Jul 23 '15 at 15:57
  • 1
    @Muzer for reasons that I can't say I entirely agree with, OSX has decided that GUI terminal emulators will run login shells. – terdon Jul 23 '15 at 18:03

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.