Difference Between bashrc and bash_profile in Linux
In the Linux operating system, both .bashrc and
.bash_profile are user-specific configuration scripts that
customize the Bash environment, yet they are triggered by entirely
different shell sessions. The key significance of distinguishing between
them lies in the difference between login and non-login interactive
shells: .bash_profile executes once during the initial
login session to set up global environment variables, whereas
.bashrc executes every time a new terminal instance is
opened to configure local shell behavior such as aliases and
functions.
Understanding Login vs. Non-Login Shells
To understand which file to use, you must first understand how Bash is initiated:
- Login Shell: A login shell requires authentication
or is explicitly invoked as a login shell. Common examples include
logging into a remote server via SSH, switching users with
su - username, accessing the system via a virtual console (TTY), or runningbash --login. - Non-Login Interactive Shell: A non-login shell
starts after the login process. The most common example is opening a new
terminal window or tab within an already authenticated graphical desktop
environment (like GNOME or KDE), or running a subshell by typing
bashwithin an active terminal.
The Role of .bash_profile
The .bash_profile file resides in a user's home
directory (~/.bash_profile) and is read only by interactive
login shells. Because it runs once at the start of a session, it is
primarily used for:
- Setting environment variables that child processes should inherit,
such as updating the system
$PATHor configuring runtime directories (e.g.,JAVA_HOME). - Executing commands that only need to run once per login, such as printing system status messages or starting background services.
If .bash_profile is not present, Bash looks for
~/.bash_login and then ~/.profile as fallback
alternatives.
The Role of .bashrc
The .bashrc file also resides in the home directory
(~/.bashrc), but it is executed every time an interactive
non-login shell starts. Because non-login shells do not re-run
.bash_profile, .bashrc ensures that custom
commands and interactive features are loaded in every new terminal
window. It is typically used for:
- Defining custom command aliases (e.g.,
alias ll='ls -la'). - Defining user shell functions.
- Customizing the command prompt appearance (
$PS1). - Setting shell-specific options via
shoptorset.
Best Practices and Bridging the Two Files
Because users generally want their custom aliases, functions, and
prompt settings available in both login and non-login shells, standard
practice involves having .bash_profile load
.bashrc. Linux distributions frequently include the
following snippet inside .bash_profile by default:
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fiBy linking them this way:
- Place exported environment variables (such as modifications to
$PATH) in.bash_profileso they are evaluated once and inherited down the process tree. - Place aliases, functions, and terminal color configurations in
.bashrcto guarantee they are reloaded for every terminal session without unnecessarily re-evaluating heavy environment logic.