.bashrc in Linux | Explained

Do you want to perform a specific task every time you log in to the system? save a custom function in the .bashrc file, which runs automatically to do the desired task when the system starts. The bashrc file helps the users to customize several things, such as changing the terminal color, setting the history command output length, checking the window size, and many more.

This article will elaborate on the .bashrc and its uses with the following timeline:

What is .bashrc in Linux?

The .bashrc file is a hidden configuration file in the home directory executed when a user logs in to the system. To check the .bashrc file in the home directory, execute the below “ls” command:

$ ls -l .bashrc

The output shows the .bashrc file details.

The .bashrc file contains the built-in functions and aliases, which automatically execute while logging in to the operating system. To check the content of the .bashrc file, use the below cat command:

$ cat .bashrc

The output will display the customized functions which run at the time of logging in to the system. Let’s get to .bashrc uses.

How to Use .bashrc in Linux?

The .bashrc file can be customized to run the desired functions. Let’s check the uses of .bashrc with examples.

Three simple steps are followed to make the changes in the .bashrc file, which are:

  • Open the .bashrc file in your desired editor ( in this case, nano) using this command:
$ nano .bashrc
  • After opening the .bashrc file, add the custom function on the last line and save it.
  • To save the changes permanently in the .bashrc file to run this function automatically, use the below command:
$ source .bashrc

Let’s elaborate on the usage of the .bashrc file with several examples.

Example 1: How to Create a Custom Function in .bashrc to Display the username?

Follow the steps below to create a custom function that displays the user name.

Step 1: Open the .bashrc File

First, Open the .bashrc file with this command (nano editor used):

$ nano .bashrc

Step 2: Add Function at the End

Add a custom function that displays the user name. For instance, to create a function named “username”, which displays the currently logged-in user name, add the below code at the end of the .bashrc file:

username()
{
echo "The Current Logged-in User is: $USER"
}

Press the “Ctrl + O” key to save a file and then hit the “Enter” button and press the “Ctrl + X” to exit the nano editor.

Step 3: Reload the .bashrc File

To reflect the changes in the .bashrc file, use the below source command:

$ source .bashrc

Step 4: Verification

To verify the “username” custom function, write the function name “username” in the terminal and hit enter:

$ username

The output shows the text with the user name “ubuntu”, as done in the customized “username” function.

Example 2: How to Update and Upgrade Using .bashrc?

Following the steps explained above, this example creates a user-defined function to perform an update and upgrade on the system.

For instance, the “UpdateUpgrade” function is created as below, which performs both updates and upgrade tasks by just writing the function name. Add the “UpdateUpgrade” function at the end of the .bashrc file:

UpdateUpgrade ()
{
  sudo apt update  && sudo apt upgrade
}

To save the changes of the UpdateUpgrade function in .bashrc, use the source command as discussed below:

$ source .bashrc

Now, to verify the working of the UpdateUpgrade function, write the customized function name in the terminal as done below:

$ UpdateUpgrade

The output shows that the packages started updating and upgrading by simply using the name of the function.

Example 3: How to Create Aliases Using .bashrc?

The aliases are used to create shortcuts for the long commands. Several built-in aliases can be found using:

$ alias

The above output shows the built-in aliases. For instance, the “l” is the alias of the “ls -CF” which helps to show different colors for directories, files, and scripts while outputting the “ls” command.

Similarly, we can create aliases for several tasks with a single command. The general syntax for creating the aliases is given below:

$ alias <new-command>=<old-command>

In the following command, an alias named “lt” is created for the “ls -ltr”, which provides a long list of the files in the current directory (l), for time (t) and sorting it in reverse order (r). To do so, the following code is added at the end of the .bashrc file:

$ alias lt='ls -ltr'

Save the “lt” alias changes using “source .bashrc” in the .bashrc, and verify this alias as shown below:

$ source .bashrc 
$ lt

The output shows the “lt” command works as the “ls -ltr” command, performs the long list (l), time sort (t), and reverse sort (r) with a single command.

Example 4: How to Change the Username/Hostname Color on Terminal?

The .bashrc file can be edited to change the default color for the username and the hostname on the terminal. The default color for most Linux terminal are green, if you want to customize it to your specified color, you can use the following color code in the below performed examples:

ColorCode
Green32
Blue34
Cyan36
Red31
Purple35
Brown33

Let’s change the terminal color from default green to “Red”. To change the terminal color from Green to Red, the below code (31 represents Red) is added at the end of the .bashrc file:

$ PS1="\[\033[31m\]\u@\h:\w$"

[\033[m] = Reset all styles and colors

\u = Represents the logged-in user name.

\h = Hostname.

\w = The current working directory.

$ = Displays ‘#’ for the root user with “0” UID, else for normal user show ‘$‘ character.

Let’s update the changes in the .bashrc file using the source command as shown below:

$ source .bashrc

As the changes are saved in the .bashrc file, the terminal color is changed to “red” as seen in the output.

Similarly, to change the terminal color from red to blue, add the below line (blue: 34) in the .bashrc file:

$ PS1="\[\033[34m\]\u@\h:\w$"

Use the source command to save the changes in .bashrc to see the terminal color changing to “blue”:

$ source .bashrc

The color has been changed to blue.

Conclusion

The .bashrc is a hidden configuration file that executes when the user logs in. We can create the custom functions and aliases in the .bashrc file to automatically run when the system is turned ON instead of running it manually every time. This post has provided all the details, working, and usage of the .bashrc file in Linux.