Understanding Linux .bashrc and .bash_profile files

Bashrc & .bash Profile

Shells are an important part of any Linux user session. We are provided several different types of shells in Linux to accomplish tasks. Each shell has its unique properties. Hence, there are many instances where one shell is better than the other for specific requirements.

The most commonly used shell in Linux systems is the GNU Bourne-Again Shell. Better known as the bash shell, it is the default shell in a wide variety of Linux distributions today. In this tutorial, we will discuss two major scripts which are run when we invoke the bash shell.

What is the .bashrc File in Linux?

The .bashrc file is a bash shell configuration file.

It is stored at ~/.bashrc and gets executed every time a user starts up a fresh terminal session in interactive mode on their Linux system.

As the dot suggests, this is a hidden file in Linux. The .bashrc file contains a set of data that defines all the configurations for a terminal session. These configurations include setting up or editing things like the environment variables, shell history, completion, command aliases among others.

Find Bashrc
Find Bashrc

When we use the find command to search for the .bashrc on our system, we get an output similar to the one above. Here, the first file is a template that is used whenever a new user is created. The second file is the .bashrc file for the current user in the screenshot. The third file is the .bashrc config file for the root user.

Bashrc File
Bashrc File

This is how a .bashrc file looks when opened with a text editor. This file is used to initialise the interactive terminal session on your system. A detailed look into the contents will show you all the configurations for your interactive bash shell session. Let’s see how can we take advantage of the .bashrc file on our system.

What is the use of the .bashrc file?

The most common use of the .bashrc file is to set up custom environment variables for different users. It helps a user set up their preferences for the bash shell.

Being a shell script, the .bashrc file can be used to do virtually anything that a user is allowed to do.

Being invoked as soon as the bash shell is opened in the interactive mode, the .bashrc file can be used to set up custom commands for your personal use.

Further, you can set up aliases for your bash session. It can limit the length of shell history as well. Essentially, by editing the .bashrc file using any text editor, a user can set system parameters and create environment variables in a way where their customizations will be available in every future session. 

What is the .bash_profile file in Linux?

The .bash_profile file is another bash shell script file which we can see as a config file. It is stored at ~/.bash_profile.

However, unlike the .bashrc file, it gets executed every time a user logs into a system.

In simple words, it is invoked when you enter your username and password to log in on your Linux system. This login can be both local or remote. It is different from a regular bash shell invocation and is seen as a login shell mode session. 

Like we discussed earlier, the dot signifies that this is a hidden file. It contains a set of data which defines all the configurations for the current login shell for the user. These configurations include setting up or editing things like the environment variables, shell history, completion, command aliases among others. Each user has their own .bash_profile file which stores all the configurations for the particular user. Whenever we create a new user on our system, Linux will generate a new default .bash_profile file for it.

Bash Profile 1
Bash Profile 1

This is how a .bash_profile file looks when opened with a text editor. This file is used to initialise the current user session on your system. A detailed look into the contents will show you all the configurations for your bash login shell session. Let’s see how can we take advantage of the .bash_profile file on our system.

What can we do with the .bash_profile file?

The most common use of the .bash_profile file is to set up custom environment variables for different users. In practice, the usage of the .bash_profile file is the same as the usage for the .bashrc file. Most .bash_profile files call the .bashrc file for the user by default. Then why do we have two different configuration files? Why can’t we do everything using a single file?

Well, the short answer is freedom and convenience. The longer answer is as follows: Suppose, You wish to run a system diagnostic every time you log in to your Linux system. You can edit the configuration file to print the results or save it in a file. But you only wish to see it at startup and not every time you open your terminal. This is when you need to use the .bash_profile file instead of .bashrc

Conclusion

Shells are one of, if not the most powerful tools available to a Linux user. Without shells, it is practically impossible for a person to utilise the features and functionality offered by the kernel installed on their system. With the bash shell configuration files such as .bashrc and .bash_profile, we can customise our experience using the shell to suit our needs to a fine degree. It can help us make our work efficient and easier.

We hope this tutorial was able to help you to get understand the concept the .bashrc and the .bash_profile file, along with how we can make the most out of these configuration files. If you have any feedback, queries or suggestions, feel free to reach out to us in the comments below.