how to reload shell profile without restarting the command prompt in linux

In Linux there are several options that can be set in the shell profile or shell environment, such as the environment variables. The environment variable can be the path, the classpath or prompt display among various others. Every time you edit one of the shell profile files, you need to reload shell profile or the command shell itself for the modifications to take effect.

You can add, update and remove these variables directly from the command line most of the times. But if you want any of these changes to be persistent across reloads and reboots then you need to set them in the shell environment using these shell startup or profile files.

One option is to close the prompt and start another or new command window. This will execute the shell scripts again to load the modified version. But most times you just want to load the new modified shell profile without having to restart the command prompt or the command window. This is especially useful when working remotely, and eliminates the need to logout and log back in.

Profile files for bash shell: The bash shell usually has two profile files, where you could configure your environment,  .bash_profile and .bashrc.

Profile files for csh shell: The csh shell also have two startup or profile files, .cshrc and .login

There are two different commands that can be used to execute and reload shell startup scripts, source and . (dot command). You will need to execute these in each and every open command prompt in order to load them into that particular shell environment.

Using source

source ~/.bash_profile

or

source ~/.bashrc

Using the . (dot)

. ~/.bash_profile

or

. ~/bashrc

Note: The above command examples reads as a ‘dot’ followed by a ‘space’ and then the relevant file.

Also note that the above examples use the bash specific files but you can use the same commands to reload shell with the files for csh or other shells as well.

A drawback of just reloading the shell is that it usually works best if you are adding, modifying or updating the environment. This will not however remove the values that has previously been set but has since been removed from the script file. You will need to explicitly unset or remove those environment variables at the command line.

One workaround to this issue is to re-login into the same shell.  This however opens (or starts) a new shell over the current one, and is theoretically quite different from just reloading the script files and a reload shell environment as in the previous examples. You can use the su command to re-login.

su - <username>

The above command will re-login and execute the profile and login scripts. You need to specify the (hyphen) after su to load the new or modified files. The username in the above example refers to the same username as you are currently using or the one that you have modified the shell scripts for.

Most of the time you will need to modify these scripts only when you are setting up your shell or environment. But if for some reason you find yourself modifying these files routinely and on a regular basis, you can set up an alias to reload shell, in the same profile file that is being reloaded, as shown below.

alias br=". ~/.bash_profile"

Reload shell using either the source or the . command as mentioned in the previous examples. After that you can just use the alias br in the command line prompt to reload the shell profile.