2142

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

Jesse Nickles
  • 1,435
  • 1
  • 17
  • 25
Jed Daniels
  • 24,376
  • 5
  • 24
  • 24

18 Answers18

3401

You can enter the long form command:

source ~/.bashrc

or you can use the shorter version of the command:

. ~/.bashrc
Harry B
  • 2,864
  • 1
  • 24
  • 44
George Hawkins
  • 37,044
  • 7
  • 30
  • 43
  • i seem to keep having to do this every time i open terminal... is there a way to automate it? – Alex Apr 07 '12 at 14:39
  • 114
    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 01:09
  • what is that "command"? I did "man command", but it has no info about it. Searching gives no info either http://www.yandex.com/yandsearch?text=linux+man+source&lr=87 . Is it a command at all? – Tebe Sep 06 '13 at 19:46
  • 1
    @gekannt - if you do "type source" you'll see that source isn't a normal command but rather a "shell builtin" - this means you won't find an executable for it somewhere in the file system, e.g. in /usr/bin, instead it is implemented as part of the shell. So if you do "man bash" and search for the section "SHELL BUILTIN COMMANDS" and then search for "source" you'll find a good explanation of what source does (and if you understand fork as well then it should become clear why source is a builtin rather than a command). – George Hawkins Sep 09 '13 at 10:16
  • 9
    @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 09 '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 08:55
  • 5
    I would also recommend creating an alias (which you could store in ~/.bashrc or ~/.bash_aliases) that opens .bashrc, and reloads it after the editor exits. You can do it by combining two commands in an alias, for example like so (if vim is your preferred editor, otherwise swap it out to something else): ```alias editbashrc='vim ~/.bashrc; source ~/.bashrc'```. This will make the editing much smoother, since you don't need to think about the reloading, after doing the edit, if using the custom alias. – Samuel Lampa Nov 12 '14 at 07:40
  • 15
    It will affect **only** the current terminal. – matepal297 Mar 16 '15 at 23:26
  • I'm using this alias to edit bashrc for all users: ***alias bashrc="sudo nano /etc/bash.bashrc; source /etc/bash.bashrc"*** – Tarik Aug 20 '17 at 14:43
  • 2
    I think the answer below from @WhoSayIn should be preferred over this to prevent the PATH problem discussed above: `exec bash` – zenw0lf Dec 23 '17 at 23:28
  • Seems `source ~/.bashrc` this throw out of current python virtualenv. – mrgloom Jul 01 '19 at 18:22
  • This will only be valid for current terminal, whether using "fresh start" exec bash, or "ad-hoc" source ~/.bashrc. I am currently working from home, I want to be able to SSH in to my remote host and have all updated settings without executing these commands each time I make a new SSH connection. I cannot access office to perform on-site log out/restart, due to practical and bureaucratic reasons. Any suggestions? – MahNas92 Jul 06 '20 at 16:44
  • @HighCommander4 another way of ensuring that both `foo` and `bar` are in the path if `PATH=$PATH:foo` is already entered is to add `PATH=$PATH:foo:bar`. This should include both. Ideally, you would want to go in and erase the first declaration, but as you point out, it is overwritten anyway. Erasing just makes it cleaner and supports readability. – Nate T Oct 02 '21 at 05:35
  • @MahNas92 If you open a new terminal, you do not need to do anything. Once it is sourced, any terminal opened will automatically include it. For previously opened terminals, it needs to be sourced from each. The easiest option, imo, is to broadcast all and reset or source. – Nate T Oct 02 '21 at 05:39
  • This seems to work great for my purposes. I haven't noticed any edge cases yet. Will update here if so. Thank you! – no_one Oct 29 '21 at 02:37
  • You can also add a line to your `.bashrc`: `alias RELOAD=". ~/.bashrc"` – bobobobo Jun 15 '23 at 01:20
381

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).

Jonathan Gilbert
  • 3,526
  • 20
  • 28
WhoSayIn
  • 4,449
  • 3
  • 20
  • 19
  • 17
    Could you please explain the difference of `source .bashrc` command and `exec bash`? – muradin Nov 20 '14 at 07:42
  • 26
    @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
  • 2
    It's great especially because the bashrc may be updated either in `/etc` or `/home` directory, and we don't need to care about this. – Han Mar 31 '15 at 10:07
  • 6
    In my hyper-specific circumstance, this totally rocked. My Dockerfile executes an install script that modifies .bashrc. I then need that to reload, but `. ~/.bashrc` will execute in `dash` rather than `bash`, so there is an error because `shopt` is missing. `source` isn't found from the shell, so that solution is out as well. I tried this and the docker image built smoothly! – m59 Jun 02 '15 at 02:11
  • 1
    For CentOS7 and with helper bash configs, this was most helpful. Doing `. ~/.bashrc` did not reload my custom aliases file. – onebree Oct 07 '15 at 15:53
  • 17
    Elegant, but "does the same thing" is not entirely correct. `source ~/.bashrc` will preserve your _entire_ shell environment (though likely modified by the sourcing of `~/.bashrc`), whereas `exec bash` will only preserve your current shell's _environment variables_ (any ad-hoc changes to the current shell in terms of shell variables, function, options are lost). Depending on your needs, one or the other approach may be preferred. – mklement0 Jan 28 '16 at 22:49
  • 1
    @mklement0, thanks for the detailed information. This solution would work in many cases but I guess you may need to use -source- in situations like you mentioned. – WhoSayIn Feb 01 '16 at 12:31
  • 14
    @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
  • 1
    Some specific examples of the kind of surprise [@mklement0 mentioned](https://stackoverflow.com/questions/2518127/2518150#comment57870320_9584378): Your `pushd` / `popd` directory stack will be erased (except for the CWD), and your `PS1` prompt will be reset. Some dev environments, like Python's `virtualenv` / `venv`, alter your `PATH` and other variables, and put the name of the active environment in your `PS1` prompt. After `exec bash`, the environment variables are still active, but the visual indicator in the `PS1` prompt is gone. Also, cleanup functions like `deactivate` have vanished. – Kevin J. Chase Sep 26 '16 at 12:50
  • 2
    This works for my Cygwin + win7. Thanks! Typing this faster that typing . ~/.bashrc – Danniel Little Mar 15 '17 at 19:45
  • Will this work for another session I open after running 'exec bash' in my current session ? – nitinr708 Aug 01 '17 at 08:45
  • 1
    just to add, use: exec $SHELL -l; as $SHELL returns bash in this instance. – mirageglobe Jun 01 '19 at 23:08
  • 1
    This worked. Super nice, short, solid. `source` did not work for some reason. – lowtechsun Aug 28 '19 at 17:00
  • 1
    That worked perfectly for me when the source option wasn't working, this is exactly what I was looking for! Thank you, that way I don't have to restart terminal and use bash whenever absolutely necessary! Thanks again – Iakovos Belonias Mar 02 '20 at 12:39
  • 1
    @whosayin thx! this just helped me with executing ansible-playbooks that were no longer accepting arguments that had worked previously. – ficestat Aug 25 '20 at 22:20
  • The issue here is that you lose your entire command history. (The commands entered from that terminal, not the global history, which is practically useless anyway, imo.) – Nate T Oct 02 '21 at 05:41
  • 1
    Nice, I've been using `source` for years, but I've noticed that if I `source` my `rc` file multiple times it'd take longer with every iteration. I'm guessing there's something stacking in the background. Anyway I've replaced my old "source shell" alias with `alias ss="exec ${SHELL}"` and things behave as expected now. – theOneWhoKnocks May 16 '22 at 17:48
171

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.


Note: The above applies analogously to other shells too:

  • To apply the exec approach to whatever your default shell is, use exec $SHELL
  • Similarly, the sourcing approach requires you to know and specify the name of the shell-specific initialization file; e.g., for zsh: . ~/.zshrc

[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.

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • 1
    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 07 '16 at 08: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 01 '17 at 08:49
  • 3
    @nitinr708: Yes, `exec $BASH` will source `~/.bashrc`, so you'll see its changes to the shell environment in the new session. – mklement0 Aug 01 '17 at 12:45
  • This is why I use `broadcast all` + source. Best of both worlds, imo. – Nate T Oct 02 '21 at 05:43
  • @i_want_more_edits: `$SHELL` reflects whatever shell is the current user's _default shell_, which may or may not be Bash. – mklement0 Jan 06 '22 at 02:19
  • @mklement0 so if I called a `/path/to/different/executable/bash`, `$BASH` whould contain that path, while `$SHELL` would not mind and contain the default shell, right? – i_want_more_edits Jan 06 '22 at 11:46
  • @i_want_more_edits: Correct. – mklement0 Jan 06 '22 at 12:23
  • To add on the simplicity of the `exec $BASH` command, this command also works inside the automated shell scripts where we don't know user's actual shell type i.e. BASH/ZSH/FISH etc. If we use `source ~/.bashrc` inside the shell script then we also need to add an explicit check to find the user's shell type before you can run the `source ~/.bashrc` or `source ~/.zshrc`or `~/.config/fish/config.fish` command. – Asrar Aug 25 '22 at 10:54
57

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

. .bashrc
Randy Proctor
  • 7,396
  • 3
  • 25
  • 26
  • 32
    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
  • 58
    . 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
  • 8
    Cool. Thanks. Now that I didn't know. – Jed Daniels Mar 25 '10 at 18:49
  • 3
    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 01 '16 at 00:04
  • 1
    @RandyProctor: While it's commendable that you point out that your answer is inferior to the accepted one, it would be even better if you simply _deleted_ it, given that it's not only distinctly inferior (due to only working if the current dir. _happens to be_ the current user's home dir., as stated), but, conversely, _adds nothing_ of interest. – mklement0 Mar 24 '19 at 21:00
24

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"

Roy Lin
  • 730
  • 8
  • 17
20

Depending on your environment, just typing

bash

may also work.

James
  • 2,306
  • 1
  • 22
  • 23
  • 17
    However, this will invoke a new shell within the current one, thus wasting resources. Better use @WhoSayln's [exec](http://www.gnu.org/software/bash/manual/bashref.html#index-exec) solution which **replaces** the current shell with the newly invoked one. – Bernhard Wagner Sep 04 '13 at 09:45
  • 1
    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 Chaula May 06 '17 at 12:45
  • This is good solution whereby user privilege access is limited. – Tunde Pizzle Oct 18 '18 at 07:36
  • 1
    invoking a subprocess adds a layer of complexity that has no additional value. – Alan Berezin Jan 02 '20 at 00:07
19
. ~/.bashrc

. is a POSIX-mandated builtin


Alternatives

source ~/.bashrc

source is a synonym for dot/period . in bash, but not in POSIX sh, so for maximum compatibility use the period.

exec bash
  • exec command replaces the shell with a given program... – WhoSayIn
Geoffrey Hale
  • 10,597
  • 5
  • 44
  • 45
  • 2
    `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
11

exec bash is a great way to re-execute and launch a new shell to replace current. just to add to the answer, $SHELL returns the current shell which is bash. By using the following, it will reload the current shell, and not only to bash.

exec $SHELL -l;
mirageglobe
  • 2,446
  • 2
  • 24
  • 30
  • 1
    Just to state it explicitly: `$SHELL` reflects the current user's _default shell_, so this is a way to replace the current session - whatever shell's process it may be - with a new session of the user's default shell. `-l` makes the new session a _login_ session, which is appropriate on macOS (and by default only loads `~/.bash_profile`, not also `~/.bashrc`), but not on Linux. – mklement0 Jan 06 '22 at 02:46
10

I used easyengine to set up my vultr cloud based server.
I found my bash file at /etc/bash.bashrc.

So source /etc/bash.bashrc did the trick for me!

update

When setting up a bare server (ubuntu 16.04), you can use the above info, when you have not yet set up a username, and are logging in via root.

It's best to create a user (with sudo privileges), and login as this username instead.
This will create a directory for your settings, including .profile and .bashrc files as described on the previous ressource.

Now, you will edit and (and source) the ~/.bashrc file.

On my server, this was located at /home/your_username/.bashrc
(where your_username is actually the new username you created above, and now login with)

Vincent Doba
  • 4,343
  • 3
  • 22
  • 42
SherylHohman
  • 16,580
  • 17
  • 88
  • 94
6

Depending upon your environment, you may want to add scripting to have .bashrc load automatically when you open an SSH session. I recently did a migration to a server running Ubuntu, and there, .profile, not .bashrc or .bash_profile is loaded by default. To run any scripts in .bashrc, I had to run source ~/.bashrc every time a session was opened, which doesn't help when running remote deploys.

To have your .bashrc load automatically when opening a session, try adding this to .profile:

if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
        . "$HOME/.bashrc"
    fi
fi

Reopen your session, and it should load any paths/scripts you have in .bashrc.

karolus
  • 912
  • 7
  • 13
4

For me what works when I change the PATH is: exec "$BASH" --login

Cecília Assis
  • 139
  • 1
  • 10
  • The question is about reloading `~/.bashrc`, which `--login` will _not_ (directly) reload; at a user level, it'll reload `~/.bash_profile` (or `~/.bash_login` or `~/.profile`) instead. – mklement0 Mar 24 '19 at 21:07
2

i use the following command on msysgit

. ~/.bashrc

shorter version of

source ~/.bashrc
Sojan Jose
  • 3,168
  • 6
  • 33
  • 52
2

Assuming an interactive shell, and you'd like to keep your current command history and also load /etc/profile (which loads environment data including /etc/bashrc and on Mac OS X loads paths defined in /etc/paths.d/ via path_helper), append your command history and do an exec of bash with the login ('-l') option:

history -a && exec bash -l
beattidp
  • 53
  • 5
2

I understand you want a shell as after logging out and in again. I believe the best way to achieve that is:

exec env -i HOME="$HOME" "$SHELL" -l

exec will replace the current shell, such that you are not left with it when the new one exits. env will create a new empty environment, with -i we add $HOME so that your shell (usually bash) given by $SHELL can find ~/.profile/~/.bash_profile (and thus (on ubuntu or if specified) ~/.bashrc). Those will be sourced thanks to -l. I'm not completely sure though.

jan-glx
  • 7,611
  • 2
  • 43
  • 63
  • This works well for e.g. undoing conda init in an interactive LSF job (where you don't want to wait in the LSF queue just for a clean shell) – jan-glx May 19 '23 at 13:23
2

Be aware of $SHELL may produce unexpected results
for example connected on a Docker environment

echo $SHELL
/usr/sbin/nologin

so if you try, you will be disconnected

exec $SHELL
This account is currently not available.

so you may have to use something more complicated like

exec $(pgrep -l sh | grep "^`echo $$` " | cut -d" " -f2)

assuming that every shell contains "sh", and this command pipeline

pgrep -l sh | grep "^`echo $$` " | cut -d" " -f2

produces a complete command
if you have arguments or flags you may have to use -f2,3,4 or try

pgrep -l sh | grep "^`echo $$` " | sed -E 's/^[0-9]+ //'

But read all the advices above
Personally i dont like to loose the shell history... but it's up to you and to your needs

Lionel-fr
  • 124
  • 1
  • 1
  • 5
1

I noticed that pure exec bash command will preserve the environment variables, so you need to use exec -c bash to run bash in an empty environment.

For example, you login a bash, and export A=1, if you exec bash, the A == 1.

If you exec -cl bash, A is empty.

I think this is the best way to do your job.

CatDog
  • 415
  • 6
  • 12
0

This will also work..

cd ~
source .bashrc
Timo Tijhof
  • 10,032
  • 6
  • 34
  • 48
kirti
  • 33
  • 3
0

I wrote a set of scripts I called bash_magic that automates this process across numerous shells. If you update a shell file in the bash magic shell directory (.bash.d by default), it will automatically source the update at the next prompt. So once you've made a change, just hit the Enter/return key and any updates will be sourced.

ND Geek
  • 398
  • 6
  • 21