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:

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:

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:

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
fi

By linking them this way: