Understanding .bashrc and .profile Load Order - When Settings Are Not Applied

Understanding .bashrc and .profile Load Order - When Settings Are Not Applied

Linny-senpai, My Settings Are Not Showing Up!

Lina: Linny-senpai! I added an alias to .bashrc but it's not working even after reopening the terminal...
Linny-senpai: That's a super common problem! Once you understand when .bashrc and .profile are each loaded, everything will click.

This article answers these questions:

  • What is the difference between .bashrc and .profile?
  • Why don't my settings appear after editing .bashrc?
  • Why are my aliases missing after an SSH connection?
  • How can I reload settings without restarting the terminal?

The Role of .bashrc vs .profile

Lina: Both .bashrc and .profile look similar. Which one should I use?
Linny-senpai: The key is understanding when each one is loaded. Let's start there!
File When It Is Loaded
~/.bashrc Interactive shell (when you open a normal terminal)
~/.profile Login shell (when you connect via SSH or log in)
~/.bash_profile Login shell (takes priority over ~/.profile)

If ~/.bash_profile exists, ~/.profile is not loaded. Only one of them runs per login.

Understanding the Two Types of Shells

Lina: What exactly is the difference between a "login shell" and an "interactive shell"?
Linny-senpai: It depends on how Bash was started. A desktop terminal app and an SSH connection launch different shell types.

Login Shell

Started in these situations:

  • Connecting to a server via SSH (ssh user@host)
  • Switching to root with sudo -i
  • Switching users with su -

Files loaded:

  1. /etc/profile
  2. ~/.bash_profile (if it exists) or ~/.profile

Interactive Non-Login Shell

Started in these situations:

  • Opening a terminal app (GNOME Terminal, etc.) on the desktop
  • Starting a new shell by typing bash

Files loaded:

  1. /etc/bash.bashrc
  2. ~/.bashrc

Quick rule: Desktop terminal → ~/.bashrc. SSH connection → ~/.profile (or ~/.bash_profile).

The Full Load Order

Lina: So my .bashrc aliases are missing over SSH because the login shell never reads .bashrc?
Linny-senpai: Exactly! SSH uses a login shell, so .bashrc is not loaded automatically.
Login shell (SSH connection):
  /etc/profile
    └─ ~/.bash_profile (if it exists)
         └─ ~/.bashrc (only if bash_profile explicitly calls it)
       or
    └─ ~/.profile (if bash_profile does not exist)
         └─ ~/.bashrc (only if profile explicitly calls it)

Interactive shell (terminal launch):
  /etc/bash.bashrc
    └─ ~/.bashrc

A login shell does not automatically load ~/.bashrc.
To use your aliases and functions over SSH, you must explicitly call ~/.bashrc from inside ~/.bash_profile or ~/.profile.

Common Problems and How to Fix Them

Problem 1: Aliases Missing After SSH Login

Lina: I added my aliases to .bashrc, but after SSH-ing in I get "command not found"!
Linny-senpai: That's because the login shell doesn't read .bashrc. Add a snippet to .bash_profile to call .bashrc explicitly.

Fix: Add the following to ~/.bash_profile (or ~/.profile if ~/.bash_profile doesn't exist):

# ~/.bash_profile
if [ -f ~/.bashrc ]; then
    . ~/.bashrc
fi

Ubuntu often includes this snippet in ~/.profile by default. Check first:

cat ~/.profile
# if running bash
if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
        . "$HOME/.bashrc"
    fi
fi

If you see this output, your SSH sessions will already load ~/.bashrc.

Problem 2: Edited .bashrc but the Terminal Did Not Update

Lina: I edited .bashrc and saved it, but my open terminal still can't use the new alias!
Linny-senpai: Opening a new terminal window would work, but there's a faster way — the source command reloads the file without restarting!

Reload immediately with source:

source ~/.bashrc

Or the equivalent shorthand:

. ~/.bashrc

source (or .) runs the file inside the current shell, not in a subprocess. Changes take effect immediately — no terminal restart needed.

How to Check Your Current Shell Type

Lina: How do I know whether my current shell is a login shell or not?
Linny-senpai: Check the $0 variable — it tells you how Bash was invoked.
echo $0
# Login shell: starts with a dash (-)
-bash

# Non-login shell: no leading dash
bash

To trace which startup files are being sourced:

bash --login -x 2>&1 | head -30

Each sourced file appears prefixed with +.

What to Put Where

Lina: After all this, which file should I use for what?
Linny-senpai: Here's a simple table to guide the decision.
Setting Type Where to Write Reason
Aliases (alias ll='ls -la') ~/.bashrc Only needed in interactive terminals
Shell functions ~/.bashrc Same as above
Environment variables (PATH) ~/.profile Needed in login shells too
Prompt settings (PS1) ~/.bashrc Display settings belong in terminal config

Writing PATH in ~/.bashrc often works day-to-day, but it won't be available to programs launched without a login shell (e.g., cron jobs). Put critical environment variables in ~/.profile to be safe.

Hands-On Practice

Lina: I think I understand the theory. Can I try this myself?
Linny-senpai: Absolutely! Follow these steps one by one.

Step 1: Check your current shell type

echo $0

Step 2: Verify that ~/.profile sources .bashrc

grep -n "bashrc" ~/.profile
# Good output — .bashrc will be sourced on login
6:    if [ -f "$HOME/.bashrc" ]; then
7:        . "$HOME/.bashrc"
8:    fi

Step 3: Add a test alias and reload instantly

# Append a test alias
echo "alias hello='echo Hello Linux World!'" >> ~/.bashrc

# Reload without restarting
source ~/.bashrc

# Verify
hello
Hello Linux World!
Hint: if it still doesn't work

Check that the alias was actually appended:

tail -5 ~/.bashrc

If the alias is there but still fails, check for syntax errors in .bashrc:

bash -n ~/.bashrc

No output means no syntax errors.

Summary

Key Points to Remember

  • ~/.bashrc is loaded when you open a terminal (interactive shell)
  • ~/.profile is loaded on SSH login or system login (login shell)
  • ~/.bash_profile takes priority over ~/.profile when both exist
  • To use .bashrc settings over SSH, call it from ~/.profile or ~/.bash_profile
  • To apply edits immediately, run source ~/.bashrc (no restart needed)

Next Reading