How to Update `.bashrc` and Reload?

In Linux, the “.bashrc” is the file that executes on starting the new session of the user. This file is utilized to set the environment variables, define aliases and functions, and configure other settings of the bash shell. However, changes in the “.bashrc” file require reloading to execute in the current bash session.

This post will enlighten the methods with a step-by-step guide to update and reload the “.bashrc” file in Linux.

Method 1: Through the Nano Editor

To update the “.bashrc” file through an editor such as nano (a terminal-based text editor), follow the below-mentioned steps:

Step 1: Update/Edit the “.bashrc” File 

Open the “.bashrc” file with nano editor and update it according to user requirement. In our case, the variable name= “Henry” is exported, and the alias d=‘mkdir’ is added:

$ nano .bashrc                         #open file with editor
export name="Henry"                    #export variable
alias d='mkdir'                        #add alias

Save the “.bashrc” file by pressing “Ctrl+S” and exit from the file by pressing the “Ctrl+X.”

Step 2: Reload the “.bashrc” File

Reload the “.bashrc” file using the “source” command to use it in the current bash shell session:

$ source .bashrc

The “.bashrc” file has been executed 

Verify the Change

Check the exported variable name=“Henry” and alias d=‘mkdir’ are working that are updated in step 1:

$ echo $name
$ d Henry

The added variable and alias are working as shown above. 

Method 2: Through Redirection Operator (>>)

The user can perform any update to the “.bashrc” file through the terminal and pass it to the using the redirection operator (>>). Check this method in the below-given steps:

Step 1: Add Alias Using Redirection

Let’s add the alias update=’sudo apt update’ in the echo command to test this method. Then the echo command’s output is redirected to the “.bashrc” file using the redirection operator (>>):

$ echo "alias update='sudo apt update'" >> ~/.bashrc

The given alias is added in the “.bashrc” file.

Check the “.bashrc” File

To check if the alias is added to the file, open the “.bashrc” file using the nano editor:

$ nano .bashrc

The alias has been added in the “.bashrc” file.

Step 2: Reload the “.bashrc” File

Apply the source command to reload the “.bashrc” file:

$ source .bashrc

The “.bashrc” file has been executed.

Verify the Result 

Verify the added alias “update” in the “.bashrc” file by executing the “update” command in the terminal:

$ update

The “update” command has successfully run the system update command as defined in the “.bashrc” file.

Conclusion

To update the “.bashrc” file in Linux, open the “.bashrc” file with the nano editor, update the file, and reload it via the “source” command. Or define the alias or environment variable in the echo command, pass it to the “.bashrc” file using the redirection operator, and reload it through the “source” command.

This write-up has illustrated the methods to update the “.bashrc” file and reload it in Linux.