Understanding Linux bash profile and bashrc files

Shells are an important part of any Linux user session. We are provided several different types of shells on 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, 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 command line?

The bashrc file is a bash shell configuration file.

It is located in the user’s home directory, and bash executes it 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 on Linux. This 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 the Bashrc file

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
Contents of this file

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

Uses of the .bashrc file: Aliases and environment variables

The most common use of the .bashrc (bash read configuration) file is to set up custom environment variables for different users. It helps a user set up their preferences for the 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 shell is opened in the interactive mode, the .bashrc file can be used to set up custom commands for your personal use. You can also define functions and aliases for your shell environment, which enhances your overall shell experience.

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 for the Shell?

The .bash_profile file is another shell script file, which we can see as a config file. It is also stored in the home directory.

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 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.

You can use the ls command to list the bash_profile file, and use the cat command to see its contents. Just type the following commands in your Terminal:

ls -l ~/.bash_profile
Bash Profile 1
Bash Profile file

This is how a .bash_profile file looks when opened with the nano editor, however you can also use any other editor like Vim or Emacs. This file is used to initialize 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 environments 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 config 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 config 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

You can also use both of the files to store aliases. Alias, as the name suggests, essentially act as a syntax shortcut for long commands so that you do not have to type long commands. You can even customize your bash prompts withe colors and formats by using alias in these files. We have explored about aliases in great detail in this guide.

Conclusion: Customize your Bash Experience

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 utilize 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 customize 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 of 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.

Also Read

Interactive Shells