bash - How to reload .bashrc settings without logging out and back in again?

ID : 469

viewed : 228

Tags : bashshellterminalreloadprofilebash

Top 5 Answer for bash - How to reload .bashrc settings without logging out and back in again?

vote vote


You can enter the long form command:

source ~/.bashrc 

or you can use the shorter version of the command:

. ~/.bashrc 
vote vote


Or you could use:

exec bash 

This does the same thing, and is easier to remember (at least for me).

The exec command completely replaces the shell process by running the specified command-line. In our example, it replaces whatever the current shell is with a fresh instance of bash (with the updated configuration files).

vote vote


To complement and contrast the two most popular answers, . ~/.bashrc and exec bash:

Both solutions effectively reload ~/.bashrc, but there are differences:

  • . ~/.bashrc or source ~/.bashrc will preserve your current shell session:

    • Except for the modifications that reloading ~/.bashrc into the current shell (sourcing) makes, the current shell process and its state are preserved, which includes environment variables, shell variables, shell options, shell functions, and command history.
  • exec bash, or, more robustly, exec "$BASH"[1], will replace your current shell with a new instance, and therefore only preserve your current shell's environment variables (including ones you've defined ad hoc, in-session).

    • In other words: Any ad-hoc changes to the current shell in terms of shell variables, shell functions, shell options, command history are lost.

Depending on your needs, one or the other approach may be preferred.

[1] exec bash could in theory execute a different bash executable than the one that started the current shell, if it happens to exist in a directory listed earlier in the $PATH. Since special variable $BASH always contains the full path of the executable that started the current shell, exec "$BASH" is guaranteed to use the same executable.
A note re "..." around $BASH: double-quoting ensures that the variable value is used as-is, without interpretation by Bash; if the value has no embedded spaces or other shell metacharacters (which is likely in this case), you don't strictly need double quotes, but using them is a good habit to form.

vote vote


Someone edited my answer to add incorrect English, but here was the original, which is inferior to the accepted answer.

. .bashrc 
vote vote


With this, you won't even have to type "source ~/.bashrc":

Include your bashrc file:

alias rc="vim ~/.bashrc && source ~/.bashrc" 

Every time you want to edit your bashrc, just run the alias "rc"

Top 3 video Explaining bash - How to reload .bashrc settings without logging out and back in again?