blagus
blagus

Reputation: 2355

Restart terminal without closing on MacOS

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.

Upvotes: 85

Views: 168729

Answers (5)

Alexander Otavka
Alexander Otavka

Reputation: 978

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

Upvotes: 1

Dan Maia
Dan Maia

Reputation: 334

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.

Upvotes: 18

pranftw
pranftw

Reputation: 126

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.

Upvotes: -1

Mihir Luthra
Mihir Luthra

Reputation: 6779

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.

Upvotes: 163

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.

Upvotes: -1

Related Questions