96

When I make some changes to the shell/bash behavior, such as setting up an alias, is there a quick command to reinitialize the terminal window instead of closing and opening a new window?

8 Answers 8

90

If you mean reloading your .bashrc configuration then:

source ~/.bashrc

For less typing, you can replace source with a dot: . ~/.bashrc

6
  • 2
    Here is a question, how does this behave differently than source ~/.bashrc ?
    – crasic
    Jan 3, 2011 at 5:49
  • 4
    source is a specific to bash. . is more standard. Both work the same in bash. Jan 27, 2011 at 18:36
  • 3
    This just reloads your PATH and some environment variables. It doesn't "reset" anything. @NES's answer is the correct one.
    – Cerin
    Jun 12, 2017 at 1:46
  • 12
    This doesn't reset anything, if you had updates to your PATH you'll now have your old updates and new updates.
    – Constantin
    Aug 8, 2018 at 15:33
  • 2
    For anyone looking for an actual solution because this is not at all the same as closing and starting a new one. Everything that was exported to the env will still be there. The answer of @isarandi is what you might be looking for. Mar 22, 2020 at 18:32
65

Some Addition i found in the manpage from the reset/tset command

tset reset terminal intialization

command: reset

Tset initializes terminals. Tset first determines the type of terminal that you are using. This determination is done as follows, using the first terminal type found.

an advantage seems to be, that it's independent from the used shell. also works with fish here.

So to reinitialize any terminal just do-

$ tset

OR

$ reset
3
  • 4
    Unfortunately this is also wrong. It does not reset existing environment variables.
    – isarandi
    Mar 28, 2019 at 7:25
  • 1
    I think the full answer would be tput reset && source ~/.profile. Oct 20, 2021 at 10:28
  • Nice! It worked for me! Jan 24 at 1:55
13

An additional option to the exec bash is that if you changed your .profile (or .bash_profile), you can do

$ exec bash --login

That will read your profile again as well. It wouldn't hurt to add the -i option as well to explicitly tell bash that this is an interactive shell, but it can normally figure that out for itself.

8
  • for me it works without exec too Dec 10, 2017 at 15:52
  • 2
    Unfortunately wrong as well. Already set environment variables are still there and aren't reset.
    – isarandi
    Mar 28, 2019 at 7:26
  • 3
    @ArmenSanoyan but that would create a new chile process, with the shell calling bash --login as its parent. Using exec bash --login will replace the current shell, as explained in man bash
    – hashlash
    Dec 19, 2020 at 7:04
  • @isarandi I've found that using exec -c bash --login, the command will be executed with an empty environment. But it seems that some important env vars are not reinitialized, which makes my ~/.bashrc not executed. I think the problem lies in how the initial env vars passed to the ~/.profile script
    – hashlash
    Dec 19, 2020 at 8:09
  • 1
    @information_interchange: the old bash is still sitting around in the process list (and waste a few MiB of RAM, which is trivial these days on desktops). And when you eventually exit (or control-d) out of that shell much later, the terminal window / tab won't close and you'll be wondering if it didn't work. Jul 21, 2021 at 17:06
12

Use exec sudo --login --user $USER.

If you also want the previously entered commands to disapper (full reset of the terminal), combine it with reset as reset; exec sudo --login --user $USER.

To keep the current working directory as well, use the following function:

reinit(){
  reset
  exec sudo --login --user "$USER" /bin/sh -c "cd '$PWD'; exec '$SHELL' -l"
}

There are many answers around the web but most don't actually work. Easy way to test is to set export SOMEVAR=42 then execute the supposedly resetting command and do echo $SOMEVAR. If it's 42, the environment was not reset.

There is also exec -c bash -l or exec env -i bash -l, but these are broken, somehow the $HOME variable is not set after this.

3
  • To keep the current directory as well: exec sudo --login --user $USER sh -c 'cd '"$PWD"'; bash' Apr 12, 2020 at 19:22
  • 1
    Thanks, that's useful. See my edit, which does not assume bash and uses -l to ensure a login shell and uses exec so there are no nested shells.
    – isarandi
    Apr 13, 2020 at 13:13
  • best answer! Strangely though the PATH is not exactly the same as from a fresh terminal... Jul 31, 2020 at 0:08
10

You have to replace the running application/shell with a new instance. E.g. if you are using bash as your preferred shell type the following line in your command line ($ is the placeholder for the beginning of your command line):

> $ exec bash

The running application/shell is replaced by new instance of bash like starting from scratch. All your previous modification are gone.

Remark: Do not forget that your terminal application may be reprogrammed. You have to reset your terminal application manually.

2
  • 2
    What do you mean by "reprogrammed"? Aug 11, 2012 at 3:53
  • 3
    This answer is also wrong. exec inherits the environment from its predecessor. Hence it does not restore environment variables. Some may be reloaded by running a new bash but those that are not overwritten are not removed. Mar 22, 2020 at 18:24
0

your shell is an executable you can call. So if you're using bash you can call bash and if you're using something else like zsh you can just enter zsh

2
  • This was my thought as well, but I am worried that there will be some issues Jul 21, 2021 at 14:46
  • this will create another zsh session in an already existing one which is not happening in case of exec zsh
    – Aram
    Sep 2, 2021 at 8:30
0

Use the terminal's functions clear or screen.

0

On Ubuntu at least, . ~/.profile is better than . ~/.bashrc, because the .profile file also sources the .bashrc file, and it brings in other dirs, such as ~/bin to your PATH if those dirs exist.

You must log in to answer this question.

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