When a user logs into a system that uses Bash as its shell, several scripts can be executed depending on the type of login and system configuration.
For a login shell (e.g., logging in via console, SSH):
Bash first looks for and sources the following files in the user's home directory, in this order:
~/.bash_profile:
This is the most common file for user-specific login shell configurations. It's typically used to set environment variables like PATH, define aliases, and run commands that should only execute once per login session.
~/.bash_login:
If ~/.bash_profile does not exist, Bash will then look for and source this file.
~/.profile:
If neither ~/.bash_profile nor ~/.bash_login exists, Bash will source ~/.profile. This file is often used for settings that are common across different shells (not just Bash).
Additionally, system-wide login scripts are executed before the user-specific ones:
/etc/profile:
This file is executed for all users upon login and contains system-wide environment settings and commands.
/etc/profile.d/:
Many distributions use this directory to store individual script files that are sourced by /etc/profile, allowing for modular system-wide configurations.
For a non-login interactive shell (e.g., opening a new terminal window within a graphical environment):
~/.bashrc: This file is sourced for interactive non-login shells. It's typically used for shell customizations like prompt settings, aliases, and functions that should be available in every new terminal session.
Important Note: It's common practice for ~/.bash_profile (or ~/.profile) to explicitly source ~/.bashrc to ensure that configurations from ~/.bashrc are also available in login shells. This is often done with a line similar to:
Code
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi