Linux shell configuration files—.bashrc, .bash_profile, and .profile—control how your command line environment behaves. Each file serves a distinct purpose based on shell type and session mode. Understanding these differences helps you set up aliases, environment variables, and startup commands correctly.
.bashrc: Interactive Non‑Login Shell Configuration
The .bashrc file is executed each time you start a new interactive non‑login shell—for example, when you open a terminal window or a new tab. It exists at two levels:
- System‑wide:
/etc/bashrc(applies to all users) - User‑specific:
~/.bashrc(applies only to the current user)
Use .bashrc for setings that should apply every time you launch a fresh terminal session, such as:
- Aliases and shell functions
- Environment variables limited to the current shell session
- Customizing the prompt (
PS1) - Modifying the search path (
$PATH)
Example entry:
alias ll='ls -la'
export PATH="$HOME/bin:$PATH"
.bash_profile and .profile: Login Shell Configuration
.bash_profile and .profile are read when you start a login shell—such as when logging in via the console, SSH, or a graphical login manager. The system‑wide equivalent is /etc/profile, which sets global environment variables and executes scripts in /etc/profile.d/.
/etc/profile– affects all users~/.bash_profile– affects only the current user (Bash shell)~/.profile– affects only the current user (compatible with other sh‑style shells)
These files are ideal for:
- Setting environment variables that persist throughout the entire login session
- Launching background processes or applications at login
- Sourcing other configuration files, such as
.bashrc
Example that ensures .bashrc settings are applied in a login shell:
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
Key Differences and When to Use Each
Scope:
/etc/profileand/etc/bashrcare system‑wide.~/.profileand~/.bashrcare user‑specific.
Loading Order for a Login Shell:
/etc/profile/etc/profile.d/scripts and/etc/inputrcif referenced~/.bash_profile(tried first)~/.bash_login(falllback)~/.profile(fallback)~/.bashrc(often sourced from one of the above)
Execution Frequency:
.bashrc– runs on every new interactive non‑login shell (every terminal window/tab)..bash_profile/.profile– runs only once at login.
Shell Compatibility:
.bash_profile– Bash‑specific..profile– works with any Bourne‑compatible shell (sh, Bash, Zsh, etc.).
Recommendations:
- Put environment variables in
~/.bash_profile(or~/.profilefor non‑Bash shells) and source~/.bashrcfrom there. - Put aliases, functions, and prompt customizations in
~/.bashrc. - Keep portable settings in
~/.profileand Bash‑specific ones in~/.bashrc.
By using these files correctly, you ensure that you're shell environment is consistent whether you log in remotely, open a terminal, or run a script.