If I make changes to .bashrc
, how do I reload it without logging out and back in?
16 Answers
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).
To complement and contrast the two most popular answers, . ~/.bashrc
and exec bash
:
Both solutions effectively reload ~/.bashrc
, but there are differences:
. ~/.bashrc
orsource ~/.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.
- Except for the modifications that reloading
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 not likely in this case), you don't strictly need double quotes, but using them is a good habit to form.
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.
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 privledges), and login as this username instead.
This will create a directory for your settings, including .profile and .bashrc files.
https://linuxize.com/post/how-to-create-a-sudo-user-on-ubuntu/
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)
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
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.