If I make changes to .bashrc, how do I reload it without logging out and back in?

share|improve this question

10 Answers 10

up vote 1761 down vote accepted

You just have to enter the command:

source ~/.bashrc

or you can use the shorter version of the command:

. ~/.bashrc
share|improve this answer
38  
+1 for absolute path to bashrc – N 1.1 Mar 25 '10 at 18:07
48  
This is not exactly the same as logging in and back out. Say you had the following line in .bashrc: export PATH=$PATH:foo, and then you change it to export PATH=$PATH:bar. If you log in and back out, only bar will be in the PATH, but if you do what you suggest, both foo and bar will be in the PATH. Do you know of a way around this? – HighCommander4 Apr 25 '13 at 1:09
6  
@HighCommander4 - a very unsatisfactory way to sort of do what you want is to do "bash -l" however this actually creates a new subshell and when you logout you'll return to the enclosing shell where "foo" is still in PATH. If you're just interested in PATH, you could do "unset PATH" and reconstruct it from scratch, but probably easier/safer is to do "PATH=/bin:/usr/bin" before sourcing your .bashrc. How the PATH variable is built up on login is actually reasonably complex, involving input at the very least from login (see "man login") and /etc/profile (see "man bash"). – George Hawkins Sep 9 '13 at 10:36
2  
@Alex you can automate it by adding the line ~/.bashrc into ~/.bash_profile, though I don't know if this is a good practice. – Vivek Gani Oct 24 '13 at 8:55
4  
It will affect only the current terminal. – 97397654 Mar 16 '15 at 23:26

or you could use;

exec bash

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

exec command replaces the shell with given program, in our example, it replaces our shell with bash (with the updated configuration files)

share|improve this answer
5  
I think this one is the most elegant one. – David 天宇 Wong Aug 11 '14 at 7:02
10  
bash inception... – SEoF Sep 11 '14 at 14:23
6  
Could you please explain the difference of source .bashrc command and exec bash? – muradin Nov 20 '14 at 7:42
11  
@muradin, source is a built-in shell command that executes the content of the file passed as argument, in the current shell. So in your example, it executes .bashrc file in the current shell. And exec command replaces the shell with a given program, in your example, it replaces your shell with bash (with the updated configuration files) – WhoSayIn Nov 24 '14 at 13:15
5  
@SEoF, when you say "bash inception" and if you are thinking what I think you are thinking, I must say that you are wrong. Unlike the movie, you don't keep on going into bash from bash when you repeatedly do exec bash. The exec command replaces the shell with the program, in our case, bash. So, there is always one instance of bash in existence in the terminal. – John Red May 20 '16 at 17:07

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

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

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

    • Except for the modifications that reloading ~/.bashrc into the current shell (sourcing) makes, the current shell 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 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 not likely in this case), you don't strictly need double quotes, but using them is a good habit to form.

share|improve this answer
    
You answered my question before I could ask it. This is good to know; I often set my CLASSPATH for a single session. – swinefish Apr 7 '16 at 8:22
    
So even if I call exec "$BASH" will the variables that .bashrc sets be found in the shell I open next (using the same executable as my current session) ? – nitinr708 Aug 1 '17 at 8:49
1  
@nitinr708: Yes, exec $BASH will source ~/.bashrc, so you'll see its changes to the shell environment in the new session. – mklement0 Aug 1 '17 at 12:45
    
What are the quotes around $BASH for? Are they necessary? – jena Oct 10 '17 at 19:33
    
@jena: Please see the update to the footnote in the answer. – mklement0 Oct 10 '17 at 20:00

You can try do this:

. .bashrc
share|improve this answer
21  
This will only work if your current directory is actually your home directory. The following will work: . ~/.bashrc – Brian Showalter Mar 25 '10 at 18:09
6  
What makes this work? What is actually happening when I do ". .bashrc"? Thanks! – Jed Daniels Mar 25 '10 at 18:34
50  
. is a BASH shortcut for the "source" builtin command. So ". .bashrc" is the same as "source .bashrc" to the BASH interpreter. – Brian Showalter Mar 25 '10 at 18:45
7  
Cool. Thanks. Now that I didn't know. – Jed Daniels Mar 25 '10 at 18:49
1  
I just submitted an edit request to add ~/, but since the top answer shows both source ~/.bashrc and . ~/.bashrc I wonder if this answer should just be deleted as redundant. – Max Ghenis Dec 1 '16 at 0:04

Depending on your environment, just typing

bash

may also work.

share|improve this answer
13  
However, this will invoke a new shell within the current one, thus wasting resources. Better use @WhoSayln's exec solution which replaces the current shell with the newly invoked one. – Bernhard Wagner Sep 4 '13 at 9:45
    
yeah just use source. This is entirely unnecessary and annoying. – dylnmc Sep 23 '14 at 20:50
    
In addition to @BernhardWagner's comment, you also loose your current bash history if your spawn a new shell – peter May 6 '17 at 12:45

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"

share|improve this answer

TL;DR

. ~/.bashrc

TMI

. ~/.bashrc

. is a BASH shortcut for the "source" builtin command. – Brian Showalter

source ~/.bashrc

Alternative

exec bash
  • source is a built-in shell command that executes the content of the file passed as argument, in the current shell.
  • exec command replaces the shell with a given program...
  • – WhoSayIn
share|improve this answer
1  
exec bash still inherits the environment of the current shell. exec env -i bash would be closer (or exec env -i bash -l if you are currently in a login shell). – chepner May 16 '17 at 20:42

type:

source .bashrc
share|improve this answer
    
Again, works only if you are in the home directory, or more precisely, in the directory where .bashrc is located. A more correct way to do this, as told in the accepted answer, is source ~/.bashrc. – John Red May 20 '16 at 17:12

i use the following command on msysgit

. ~/.bashrc

shorter version of

source ~/.bashrc
share|improve this answer
5  
This has been the accepted answer for 4 years? – jwg Oct 9 '14 at 13:29
    
@jwg the accepted answer is . .bashrc . will work only if you are in the home directory on msysgit. – Sojan V Jose Oct 9 '14 at 17:15
    
@jwg ok i thin i was mentioning that you don't have to type 'source ~/.bashrc' instead use the shorter version . – Sojan V Jose Oct 9 '14 at 17:21
    
@jwg okay i added it as edit to the original answer :) – Sojan V Jose Oct 9 '14 at 19:28
    
Why the redundant answer @Sojan ? – nitinr708 Aug 2 '17 at 14:24

This will also work..

cd ~
source .bashrc
share|improve this answer
7  
It does, but it also changes the working directory to ~, which is not wanted. – Albin Jun 3 '14 at 15:37
1  
Thank you for keeping the context alive @Krinkle – nitinr708 Aug 3 '17 at 10:51

protected by Aniket Thakur Aug 1 '15 at 4:58

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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