Difference Between /etc/profile, ~/.bash_profile, ~/.profile, ~/.bashrc, etc

Linux TLDR
Last Updated:
Reading time: 4 minutes

The bash or any other shell uses multiple profiles, also known as shell configuration files, like β€œ/etc/profileβ€œ, β€œ~/.bash_profileβ€œ, β€œ~/.profileβ€œ, β€œ~/.bash_loginβ€œ, β€œ~/.bashrcβ€œ, β€œ~/.bash_historyβ€œ, and β€œ~/.bash_logout” to configure the user’s interactive login or non-login shell.

FilesDescription
/etc/profileIt stores the variables, aliases, functions, etc. that are applied for all users
~/.bash_profileIt loads the commands that need to be executed only once for the current shell environment
~/.profileIf β€œ~/.bash_profile” does not exist, the system will look for this file
~/.bash_loginIf β€œ~/.profile” does not exist, the system will look for this file
~/.bashrcAll the commands (like variables, aliases, and functions) in this file will be loaded only for the interactive, non-login shell
~/.bash_historyIt stores the history of user typed commands at the terminal and writes this file when the user exits the shell
~/.bash_logoutLogin shell cleanup file executed when a login shell exits

This configuration file is divided into two major categories.

  • System Profiles
  • User’s Profiles

But before understanding them, you must also be aware of interactive login and non-login shells.

Interactive Login and Non-Login Shell

When you launch the Bash, it reads and executes multiple profile files depending upon whether the shell is launched as an interactive login or non-login shell.

Interactive vs non-interactive shell

Interactive shells are shells that read and write to a user’s terminal (operated by you), and non-interactive shells are shells that are not associated with a terminal, mostly when you execute a shell script.

This script does not execute any startup file; instead, it inherits the environment variables from the shell that created it.

An interactive shell can be either a login shell or a non-login shell

An interactive login shell is launched for the first time after system boot or using the β€œβ€“login” flag, and remote access via SSH is also considered a login shell.

Whereas, an interactive non-login shell is when the user opens a sub-shell within the existing shell using the bash command or opens a new terminal tab.

System Profiles

Major shells like Bash, ZSH, and Fish will look for system profiles located at β€œ/etc/profile” and then source them in your current environment.

This profile is used to permanently set variables, aliases, functions, and handle critical components globally for all users, like the PS1 configuration, the PATH variable, and many more in your system.

This file is handled by your system (and only the root user has permission to perform any changes) to configure all new or existing user shell environments, so you do not have to interact with it unless you are sure about it.

User’s Profiles

The user’s profiles are used to set variables, aliases, and functions for specific users.

For example, when you launch the interactive login shell, Bash will first look for the system profile located at the β€œ/etc/profile” path and run all the commands in this file in order.

Then it will look for the following user’s profile, as listed in order:

  • ~/.bash_profile (if it does not exist, then β€œ~/.profile” will be looked at; if it also does not exist, then β€œ~/.bash_login” will be looked at).
  • ~/.bashrc_history
  • ~/.bashrc_logout

Once your interactive login shell is launched, you can launch multiple interactive non-login shells by executing the bash command in your parent shell or by opening new terminal tabs.

Note that in this situation, your shell will not read the β€œ/etc/profile” or β€œ~/.bash_profile” (including β€œ~/.profile” or β€œ~/.bash_loginβ€œ); instead, it will read the β€œ~/.bashrc” file to load the interactive, non-login environment variables for the current user.

The following is the flow for an interactive, non-login shell.

  • ~/.bashrc
  • ~/.bash_history
  • ~/.bash_logout

Demonstration on a Live System

Let’s look at them in the live system (I do not recommend you replicate these steps; they are only for explanation), so what I did was edit each of these files (except β€œ~/.bash_history” and β€œ~/.bash_logoutβ€œ) and echo their name at the end of the line as shown.

Editing profile files by adding their own name at the end of the file using the echo command

Now, I am operating from another system and sshing into my system, so the first interactive login shell files will be loaded, β€œ/etc/profile” and β€œ~/.bash_profile” or β€œ~/.profile” or β€œ~/.bash_login” as shown.

interactive login shell

As you can see above,

The system profile file will always be loaded first on system boot, then it will look for the β€œ~/.bash_profile” file; if it does not exist, it will then look for the β€œ~/.profile” file, and next, the β€œ~/.bash_login” file.

In my case, the β€œ~/.bash_profile” file does exist, so another two files are ignored.

Now if you open an interactive, non-login shell within the same terminal using the bash command as shown.

Interactive non-login shell

As you can see, it will ignore the interactive login shell files (β€œ/etc/profile” and β€œ~/.bash_profileβ€œ) and will only load the interactive non-login shell β€œ~/.bashrc” file.

Even if you create multiple interactive non-login shells within the same sub-shell, it will only load the β€œ~/.bashrc” file as shown.

Another example of an interactive, non-login shell

So, I hope this example will make it clear for you to understand how they work and their flow.

Difference Between the β€œ~/.bash_profile” & β€œ~/.bashrc” Files

The β€œ~/.bash_profile” file is responsible for setting user configuration settings like storing information about text editors, layout settings, etc.

It is loaded by the system profile β€œ/etc/profile” file and will only be executed once when you log in to your system.

Also remember that most of the distribution has β€œ~/.profile” instead of β€œ~/.bash_profileβ€œ.

Whereas, the β€œ~/.bashrc” file is used to store environment variables, aliases, and functions that need to be executed when you open a terminal on an already logged-on system.

So, if you want to execute something that needs to be executed only once when you boot your system, add it in the β€œ~/.bash_profile” file, or if you want something to be executed every time you launch a terminal or create a sub-shell, add it in the β€œ~/.bashrc” file.

That was the end.

I hope this article helps you understand the concept of profile files in Linux.

If you have any questions regarding this topic, feel free to ask them in the comment section.

Join The Conversation

Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.