Why to Need to Run source Command for .bashrc Alias to Get Applied?

The “.bashrc” is the script file in Linux that runs whenever the user starts a new session. This file is considered for defining the aliases to shorten the commands. However, the user needs to execute the “.bashrc” file via the source command to apply the changes in the current bash session.

This post will describe how to run an alias without applying the source command on the “.bashrc” file.

Define Alias/Aliases and Execute the “.bashrc” File Via the source Command

When the bash terminal is loaded, it executes the “.bashrc” file to refresh the terminal environment. However, the source command is considered in order to re-execute the “.bashrc” file to apply the changes in the terminal environment. 

The following steps are the implementation of defining the alias in the “.bashrc” file and executing the “.bashrc” file using the source command.

Step 1: Open the “.bashrc” and Define the Alias

Open the “.bashrc” file with the nano editor and add some aliases as in the following case, aliases for the “mkdir” and “sudo apt update” command is added:

$ nano .bashrc                              #opens the file         
alias d='mkdir'
alias update='sudo apt update'

Save the file by pressing “Ctrl+O” and exit from the file by using “Ctrl + X

Result Before Executing the “.bashrc” File

The user can check that the defined aliases are not working yet:

$ d
$ update

Step 2: Apply the source Command

Execute the “.bashrc” file through the source command to apply changes:

$ source .bashrc

The “.bashrc” file is executed.

Verify the Result

To verify the aliases are working, open the terminal session and check the above-added aliases as shown:

$ d dir
$ update

The aliases are working perfectly.

Note: The user can use aliases in the new terminal without executing the source command. Because opening the new terminal will refresh the bash session and re-execute the bash file.

Conclusion

In Linux, the source command executes the “.bashrc” file to apply the new changes in the current bash terminal session. Open the “.bashrc” file, define the alias/aliases, and then apply the source command to execute the “.bashrc” file to apply changes.

This write-up has demonstrated why the source command is used to run the apply alias defined in the “.bashrc” file.