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.
Files | Description |
---|---|
/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.
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.
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.
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.