If I make changes to .bashrc
, how do I reload it without logging out and back in?
You can enter the long form command:
source ~/.bashrc
or you can use the shorter version of the command:
. ~/.bashrc
Answered 2023-09-20 20:55:46
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? - anyone 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. - anyone 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).
Answered 2023-09-20 20:55:46
source .bashrc
command and exec bash
? - anyone 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) - anyone . ~/.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! - anyone 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. - anyone 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. - anyone 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:
~/.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).
Depending on your needs, one or the other approach may be preferred.
Note: The above applies analogously to other shells too:
exec
approach to whatever your default shell is, use exec $SHELL
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.
Answered 2023-09-20 20:55:46
exec $BASH
will source ~/.bashrc
, so you'll see its changes to the shell environment in the new session. - anyone broadcast all
+ source. Best of both worlds, imo. - anyone $SHELL
reflects whatever shell is the current user's default shell, which may or may not be Bash. - anyone Someone edited my answer to add incorrect English, but here was the original, which is inferior to the accepted answer.
. .bashrc
Answered 2023-09-20 20:55:46
~/
, but since the top answer shows both source ~/.bashrc
and . ~/.bashrc
I wonder if this answer should just be deleted as redundant. - anyone 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"
Answered 2023-09-20 20:55:46
Depending on your environment, just typing
bash
may also work.
Answered 2023-09-20 20:55:46
. ~/.bashrc
source ~/.bashrc
exec bash
exec
command replaces the shell with a given program... – WhoSayIn
Answered 2023-09-20 20:55:46
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). - anyone 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;
Answered 2023-09-20 20:55:46
$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. - anyone 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)
Answered 2023-09-20 20:55:46
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
.
Answered 2023-09-20 20:55:46
For me what works when I change the PATH is: exec "$BASH" --login
Answered 2023-09-20 20:55:46
~/.bashrc
, which --login
will not (directly) reload; at a user level, it'll reload ~/.bash_profile
(or ~/.bash_login
or ~/.profile
) instead. - anyone i use the following command on msysgit
. ~/.bashrc
shorter version of
source ~/.bashrc
Answered 2023-09-20 20:55:46
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
Answered 2023-09-20 20:55:46
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.
Answered 2023-09-20 20:55:46
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
Answered 2023-09-20 20:55:46
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.
Answered 2023-09-20 20:55:46
exec -c
the same as exec -cl
? - anyone This will also work..
cd ~
source .bashrc
Answered 2023-09-20 20:55:46
~
to change the working directory to the user home directory? - anyone 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.
Answered 2023-09-20 20:55:46