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.
|/etc/profile||It stores the variables, aliases, functions, etc. that are applied for all users|
|~/.bash_profile||It loads the commands that need to be executed only once for the current shell environment|
|~/.profile||If “~/.bash_profile” does not exist, the system will look for this file|
|~/.bash_login||If “~/.profile” does not exist, the system will look for this file|
|~/.bashrc||All the commands (like variables, aliases, and functions) in this file will be loaded only for the interactive, non-login shell|
|~/.bash_history||It stores the history of user typed commands at the terminal and writes this file when the user exits the shell|
|~/.bash_logout||Login 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.
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.
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).
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.
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.
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.
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.
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.
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.
Also Read: How to Set and List Environment Variables in Linux
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.