How to Reload .bashrc and .zshrc Without Logging Out

A Complete Guide to Reloading Shell Configuration Files in Linux

* This page contains promotional content

After editing a shell configuration file (.bashrc, .zshrc and so on) on Linux or macOS, how should you apply the changes?
Many people probably think of logging out once and logging back in, but in fact there are quite a few more efficient ways.
This article explains the various commands for reloading a shell configuration, what characterises each of them, and how to choose between them.

1. The source command - the most basic method

The source command is the most common way to run a shell script or a configuration file in the current shell environment.
With this command you can load the contents of the specified file into the current shell session without starting a new shell.

source ~/.bashrc
source ~/.zshrc
source ~/.bash_profile

The biggest advantage of the source command is that it keeps your current working environment completely intact. The current directory, the variables you have already set, the jobs that are running - everything is preserved as it is.
It is the best method when you want to check a change quickly right after editing a configuration file.


2. The dot (.) command - the POSIX standard method

The dot command is functionally equivalent to the source command, but because it conforms to the POSIX standard it is more portable.
While source is a Bash builtin, the dot command works in almost every Unix-like shell, including sh, dash and ksh.

. ~/.bashrc
. ~/.zshrc
. ~/.profile

If portability matters when you write shell scripts, or when you work in a lightweight shell environment, I recommend using the dot command rather than source. Do not forget to put a space after the dot.


3. The exec command - restarting the shell completely

The exec command replaces the current shell process entirely with a new shell.
This loads every configuration file from the beginning, so you can restart the shell in a clean state.

# Restart as a login shell (recommended)
exec $SHELL -l
exec -l $SHELL

# Restart as an ordinary shell
exec $SHELL

# Specify the shell explicitly
exec bash
exec zsh

With the -l option the shell starts as a login shell, so the files that are read at login time such as .bash_profile and .zprofile are processed as well. It is an especially effective method when you want to refresh your environment variable settings completely.

Note, however, that when you use exec you lose all the temporary variables you set in the current shell and any settings that were not exported.


4. Running the shell directly - starting a subshell

There is also a way to start a new shell directly.
In that case the current shell does not exit; a new shell starts on top of it.

bash
zsh
$SHELL

This method is easy, but be careful that the shells keep nesting. After testing a new configuration you can return to the original shell with the exit command, but if you do it many times the shells pile up on top of each other.
To log out completely you have to run exit as many times as you have nested.


5. Reloading only a specific configuration file

There are also cases where you want to load only a specific configuration file instead of reloading everything.
That way you can apply only the changes you need, efficiently.

# Reload only the alias settings
source ~/.bash_aliases
source ~/.aliases

# Reload only the environment variables
source ~/.bash_profile
source ~/.profile

# Reload only the custom functions
source ~/.bash_functions

This method is especially convenient when you split your configuration files by function and manage them that way.
Rather than loading a large configuration file every time, you can apply only the part you changed, quickly.


Comparison table of each method

Each method has different characteristics, and it is important to use them according to the situation.

MethodKeeps the current environmentProcess IDShell nestingPortability
sourceYesUnchangedNoneBash/Zsh
. (dot)YesUnchangedNoneHigh (POSIX)
exec $SHELLNoUnchangedNoneHigh
bash/zshYes (parent shell)Newly createdYesHigh

In day-to-day development work, which method you should choose depends on the situation.

When you have made a small change to a configuration file, for example when you have added a new alias, the source command or the dot command is the best fit

You can check the change immediately without interrupting your work.

On the other hand, when you have changed environment variables substantially, or when you have fixed the PATH setting, I recommend restarting the shell completely with exec $SHELL -l.
That way all the settings are loaded in the correct order and you can start from a clean state.

When you want to test a new configuration but be able to return to the previous state if there is a problem, starting a subshell is the safe option. Since you can get back to the original environment just by running exit, it is convenient when you try out experimental changes.


Summary

There are several ways to reload shell configuration files on Linux, and each has its advantages and its caveats.
A practical way to use them is source for everyday work, and exec $SHELL -l when you need a complete refresh.
If portability of your shell scripts matters, consider using the dot command instead of source.

By understanding these commands and using the right one for the job, you no longer have to repeat logging out and logging in every time you change a setting, and your work becomes substantially more efficient.

Do give them a place in your daily Linux work

See also