65

How to restart my current MacOS terminal session without closing the window?

In Linux I use exec bash but it does not work in this environment. I made a few changes to the .bash_profile (prompt, alias etc) I would like to see without closing it and opening again.

starball
  • 20,030
  • 7
  • 43
  • 238
blagus
  • 2,086
  • 4
  • 19
  • 22
  • how's about just running `terminal`(or whatever your terminal program is named -- iTerm?) from the current terminal to open a new terminal? –  Jul 29 '19 at 23:46
  • is about reseting the same terminal session, not the application nor the window – blagus Jul 30 '19 at 01:52
  • 2
    @blagus : Wouldn't `. ~/.bash_profile` achieve what you want? – user1934428 Jul 30 '19 at 05:59
  • What exactly do you mean by "reset session", and how does `exec bash` not do that? (Keep in mind, though, that most Linux terminal emulators will start a new non-login shell for each window, while macOS terminal emulators will start a login shell. `exec bash -l`, as mentioned by Mihir, would be the closer equivalent in macOS.) – chepner Jul 30 '19 at 14:03
  • I made a few changes to the .bash_profile (prompt, alias etc) I would like to see without closing it and opening again. `exec bash` only works at Linux – blagus Jul 30 '19 at 14:39
  • 1
    @blagus, I also use `macOS` and i guess `exec bash -l` works pretty well for the scenario. – Mihir Luthra Jul 30 '19 at 17:42
  • 2
    If all you need to do is reload changes from your `.bash_profile` - try `source ~/.bash_profile`. https://stackoverflow.com/questions/4608187/how-to-reload-bash-profile-from-the-command-line – nwxdev Jul 30 '19 at 19:55
  • I do think `exec bash -l` will work but I am away from my Mac right now. Soon I'll validate at home – blagus Jul 30 '19 at 20:17

5 Answers5

118

Just type in the command:

exec bash -l

I guess that should do it.

For zsh,

exec zsh -l

This is needed because every shell on macOS by default is a login shell.

Justing writing exec bash would replace the current shell with a non-login shell which is not the same effect as closing and re-opening the terminal.

exec would make new bash -l process replace the current shell. If exec is not used, bash -l would spawn a new shell over the current shell incrementing the $SHLVL.

Mihir Luthra
  • 6,059
  • 3
  • 14
  • 39
13

For me none of the other solutions work for ZSH.

Simply source ~/.zshrc did the job actually.

Note: running exec zsh -l outputs /Users/my_username/.zprofile:3: command not found: yarn (where my_username is my username). But running only the command mentioned above does the job.

Dan Maia
  • 177
  • 2
  • 9
  • 2
    I think that simply means that your `.zprofile` calls `yarn` when it is not in path or not installed (which you will need to fix). I have not been in touch with `macOS` from sometime but AFAIK, every shell is a login shell there. So, you should be getting the above error on opening every new tab if you are on `macOS`. If you are not on `macOS`, simply do `exec zsh`. – Mihir Luthra Sep 15 '21 at 18:01
  • 1
    Also, sourcing `.zshrc` again is not always a good idea. You are basically running the code in `.zshrc` twice. Many tools add their initialisation code to `.zshrc` and some of them may not be able to tolerate their code being run twice. I mean, they should generally handle it but not everyone does. A better idea is to clear the effects caused by previous run and restart again: `exec zsh` or `exec zsh -l` – Mihir Luthra Sep 15 '21 at 18:08
1

If your session is hanging (maybe your SSH connection was interrupted), you won't be able to restart by entering a command.

On iTerm, you can navigate to "Session" > "Restart Session" in the menu bar.

You can also add a key binding for this via "iTerm" > "Preferences" > "Keys" > "Key Bindings" > "+".

  • Keyboard Shortcut: Your choice, I use Cmd-R
  • Action: "Select Menu Item..." > "Restart Session"

Keyboard Shortcut Example

Alexander Otavka
  • 948
  • 1
  • 7
  • 10
0

The actual answer, assuming you interpret the question as having the same effect at the state of the terminal session as closing and reopening Terminal would, appears to be to run the executable of the used shell to start a new session:

https://unix.stackexchange.com/a/217907/137983

zsh

If you're not on Catalina where ZSH is the default shell, it's going to be:

bash

After this, all state of the previous session (like session environment variables) will be reset. Also ZSH profile should be re-sourced I think.

Tomáš Hübelbauer
  • 9,179
  • 14
  • 63
  • 125
  • 1
    On `macOS`, every shell by default is a login shell. So no, just running the executable will not have the same effect as closing and re-opening the terminal. Just writing `bash` would invoke `.bashrc` whereas writing `bash -l` invokes `.bash_profile`. Also without `exec` the current shell will stay in place and a new shell with incremented `$SHLVL` will be spawned. – Mihir Luthra Oct 18 '20 at 00:14
0

If you've made any changes to your .bashrc and .bash_profile, then without closing the terminal you can specify alias in your .bashrc and .bash_profile as shown below to restart the terminal:

alias rest='exec bash -l;source ~/.bashrc;source ~/.bash_profile'

This command sources the .bashrc and .bash_profile again, in the sense restarts the terminal and creates a new terminal session. It works for me. Give this a try!

So, if you wanna restart the terminal, just enter rest (short for restart) in your terminal.

pranftw
  • 118
  • 1
  • 9