source ~/.bashrc | Explained

In Linux, the source command is used for multiple purposes, such as executing a group of commands from a file. On the other hand, the “.bashrc” is a file containing commands or operations run at every login. The “.bashrc” is hidden in the home directory and is customizable.

This post will elaborate on all the details about the “source ~/.bashrc” command in Linux.

What is source ~/.bashrc in Linux?

When some changes are made in the bashrc file, these changes will not be applicable until the bashrc file is reloaded or refreshed. To refresh the contents of the bashrc file, we used the source command. The source command refreshes the bashrc file, and its changes apply to the shell.

How to Use the ~/.bashrc File in Linux?

The most commonly used source command with the bashrc script file is while declaring the aliases. The aliases are used for the creation of customized commands. Here, we will show the usage of the “source ~/.bashrc” file by creating an alias in Linux.

Example: Creating an Alias

We must create the alias if we want the same output as any command with our customized command.

The general syntax for creating the alias is mentioned below:

alias [customized command]=[actual command]

In the above line, we used the keyword “alias” to tell the computer that we are going to create the alias. Then used to customize the command and equivalent it with the actual command with the help of the “=” sign.

Step 1: Add the Alias to the .bashrc File

We must write this line in the bashrc script file to keep these alias permanently. The bashrc file is the hidden file and can be opened using the text editor:

$ nano .bashrc

We have used the “.” at the prefix of the bashrc because the bashrc is the hidden file and hidden files are accessed with the “.” sign in its prefix. When the file is open, it will look:

Now, at the end of the file, type the alias of your choice:

alias ‘myhostname’=’hostname’

Save the file with the shortcut key of “CTRL+S” and exit the editor using the “CTRL+X”.

Run the Alias (Before Loading .bashrc File)

Then, run the customized command in the current shell:

$ myhostname

Command not found! This happened because we have not updated or refreshed the bashrc file to implement the new changes.

Step 2: Use the source ~/.bashrc

We will update our bashrc script to apply the changes using the source command:

$ source ~/.bashrc

Again Run the Command

Now, after refreshing the bashrc file, it’s time to run our customized command:

$ myhostname

This is the working of the source command with the bashrc file script.

That’s all about the “source ~/.bashrc” in Linux.

Conclusion

In Linux, the “source ~/.bashrc” command is used to update or refresh the changes made in the bashrc file to be applicable in shells permanently. This blog explains the “source ~/.bashrc” command by demonstrating different examples. For the reader’s understanding, the link to the detailed usage of the source command and the bashrc file script is also shared.