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

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

What you'll be able to do

  • Explain when `~/.bashrc` and `~/.profile` are each loaded
  • Find and fix the reason your aliases are missing over SSH
  • Apply a change to an already open terminal with `source ~/.bashrc`

Prerequisites (read these first)

What You'll Learn

Linny-senpai, My Settings Are Not Showing Up

Conclusion: .bashrc and .profile are read at different times. That is why aliases go missing over SSH.

Lina: Linny-senpai, I added an alias to .bashrc. It still does not work after I reopen the terminal.
Linny-senpai: That is a common problem. Which of .bashrc and .profile is read, and when. Understand that and it is solved.

This article answers these questions:

  • What is the difference between .bashrc and .profile?
  • Why do my settings not appear after I edit a file?
  • Why are my aliases missing after an SSH connection?
  • Is there a way to apply settings without restarting?

Words used here

  • Shell: the program that reads your command and runs it. This article assumes bash.
  • Settings file: a file of instructions the shell reads on start-up. .bashrc and .profile are two of them.
  • Alias: a short name you give to a longer command. Some people call it a shortcut.

A file whose name starts with . is called a hidden file. ls does not show it. Only ls -a does.

First, the way back

This article edits settings files. A mistake in one can print an error every time you open a terminal.

So copy them before you edit.

$ cp ~/.bashrc ~/.bashrc.bak
$ cp ~/.profile ~/.profile.bak

If your system has a ~/.bash_profile, copy that one too.

If something goes wrong, restore the copy.

$ cp ~/.bashrc.bak ~/.bashrc
$ source ~/.bashrc

If the terminal still works and only prints an error, those two lines put it back.

If the terminal opens and closes immediately, you can start a shell that skips the settings files. From another terminal, type this:

$ bash --norc --noprofile

From there, the cp above restores your file.

The Role of .bashrc vs .profile

Conclusion: ~/.bashrc is read when you open a terminal. ~/.profile is read at login, such as over SSH.

Lina: .bashrc and .profile have similar names. I cannot tell which one to write in.
Linny-senpai: Start by learning when each one is read. That comes first.
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)

The ~ stands for your home directoryA container that organizes files. Same idea as a "folder" on Windows or macOS.. So ~/.bashrc means "the .bashrc inside your home directory".

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

Understanding the Two Types of Shells

Conclusion: A login shell starts on an SSH connection. It reads /etc/profile and ~/.bash_profile.

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

The two names, sorted out first

  • Login shell: the shell that starts when you sign in with a username and password. SSH is the classic case.
  • Interactive shell: the shell you type commands into. It is also called a conversational shell.
  • The terminal you open on your desktop is an interactive shell that is not a login shell.

Login Shell

Started in these situations:

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

Conclusion: A login shell does not read ~/.bashrc on its own. You have to call it from ~/.bash_profile.

Lina: My .bashrc aliases are missing over SSH. Is that 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 load ~/.bashrc automatically.

To use your aliases and functions over SSH, one extra step is needed. Call ~/.bashrc yourself from inside ~/.bash_profile or ~/.profile.

Common Problems and How to Fix Them

Conclusion: When aliases fail over SSH, call ~/.bashrc from ~/.bash_profile. After editing, run source ~/.bashrc to apply it now.

Problem 1: Aliases Missing After SSH Login

Lina: I added my aliases to .bashrc. Even so, after SSH-ing in I get "command not found".
Lina: Did I write it wrong? I have checked the file many times.
Linny-senpai: The way you wrote it is fine. The cause is that a login shell does not read .bashrc.
Lina: So the file itself was never read? That never crossed my mind.
Linny-senpai: Right. So let's add a line to .bash_profile that loads .bashrc. Write it once and it happens automatically from then on.
Lina: It was about where, not how. That makes sense now.

Fix: First check whether ~/.bash_profile exists.

$ ls -a ~ | grep bash_profile
  • Nothing printed (the Ubuntu default): add the lines to ~/.profile. Do not create a new ~/.bash_profile. Creating one stops ~/.profile from being read, and the PATH Ubuntu sets up for you is lost.
  • A name printed: add the lines to that ~/.bash_profile.

The lines to add are these three:

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

Those three lines mean "if a file called ~/.bashrc exists, read it". if runs something only under a condition. -f checks whether the file exists.

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. My open terminal still cannot use the new alias.
Linny-senpai: Opening a new terminal would work. The problem is that the change never reaches a terminal you already have open.
Linny-senpai: That is what the source command is for.

Use source and the change applies right away.

source ~/.bashrc

The shorthand means the same thing:

. ~/.bashrc

source (or .) runs the file inside the current shell, not in a separate process.

The change takes effect at once. You do not have to open a new terminal.

How to Check Your Current Shell Type

Conclusion: Run echo $0. A leading dash means a login shell. No dash means it is not one.

Lina: How can I tell whether my current shell is a login shell?
Linny-senpai: Look at the $0 variable. It holds the name of the shell you are running.
echo $0
# Login shell: starts with a dash (-)
-bash

# Non-login shell: no leading dash
bash

To see which startup files are being read, use this command:

$ bash --login -x -c 'exit' 2>&1 | head -30

-c 'exit' tells the new shell to quit as soon as it starts. Without it, the command sits there waiting for input and looks frozen.

Each step that runs appears prefixed with +. The lines starting with . — such as . /etc/profile or . /home/user/.bashrc — are the settings files that were read.

What to Put Where

Conclusion: Aliases and functions go in ~/.bashrc. Environment variables such as PATHA string that describes the location of a file or directory. go in ~/.profile.

Lina: In the end, which file should I use for what?
Linny-senpai: Sorting them by purpose keeps it simple.
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 usually works for day-to-day use.

But some programs run without a login shell, such as cron jobs. There, the setting may never apply. Putting important environment variables in ~/.profile is the safer choice.

Mini Exercises: Try It Yourself

Conclusion: Shell type, whether the file is read, and an instant reload. Three exercises cover this article by hand.

Lina: I follow the theory now. I would like to try it myself.
Linny-senpai: Good. I have three exercises for you. Run them in your terminal.

Exercise 1: Find out whether the shell you are using is a login shell.

Show Hint 1 (Direction)

One variable holds the name of the shell you are running. Print its contents on screen.

Show Hint 2 (Command name)

Use echo. The variable is $0.

Show Answer
$ echo $0
bash

No leading - means it is not a login shell. That is normal for a desktop terminal. Over SSH you see -bash instead.

Exercise 2: Check whether ~/.profile contains a line that loads .bashrc.

Show Hint 1 (Direction)

One command searches a file and prints only the lines containing a given word. Ask for line numbers too.

Show Hint 2 (Command name)

Use grep. The option for line numbers is -n. The word to search for is bashrc.

Show Answer
$ grep -n "bashrc" ~/.profile
6:    if [ -f "$HOME/.bashrc" ]; then
7:        . "$HOME/.bashrc"
8:    fi

Lines like these mean .bashrc is read at SSH login too. If nothing appears, add the three lines from Problem 1.

Exercise 3: Add a test alias and make it usable without opening a new terminal.

Show Hint 1 (Direction)

Add one line to the end of the settings file. Then read that file again in the current shell.

Show Hint 2 (Command name)

Append with echo and >>. Re-read with source. The file is ~/.bashrc.

Show Answer
$ echo "alias hello='echo Hello Linux World!'" >> ~/.bashrc
$ source ~/.bashrc
$ hello
Hello Linux World!

>> adds one line to the end of a file. A single > would erase the whole file, so it is not used here.

To remove the test alias, delete the last line of ~/.bashrc and run source ~/.bashrc.

Hint: if it still doesn't work

Check that the alias was actually appended:

tail -5 ~/.bashrc

Sometimes the line is there and it still fails. In that case an error inside .bashrc may be the cause:

bash -n ~/.bashrc

This command only inspects the syntax; it does not run the file. No output means the syntax is fine.

Review

Lina: So when a setting has no effect, I should ask whether the file is read before I question how I wrote it.
Linny-senpai: Exactly. .bashrc when you open a terminal, .profile when you log in. Keep those two apart in your head.
Lina: And to reach a terminal that is already open, I use source. Got it.
Linny-senpai: That's it. When in doubt, run echo $0 to see which kind of shell you are in.

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

Today's 3-Line Summary

  1. ~/.bashrc is read when you open a terminal; ~/.profile is read when you log in
  2. When aliases fail over SSH, call ~/.bashrc from ~/.profile
  3. To reach a terminal that is already open, run source ~/.bashrc

Next Reading

Share this article

Next steps