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
- The difference between
.bashrcand.profile, and when each file is read - The difference between a login shellThe first shell started right after a user logs in. and an interactive shellAn interactive program that reads the commands you type and runs them.
- Why aliases go missing over SSH, and how to fix it
- How to apply a change right away with
source, without restarting the terminal - Which file each kind of setting belongs in: aliases, environment variables, promptA symbol (like $ or #) shown when the shell is waiting for your input.
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.
.bashrc. It still does not work after I reopen the terminal..bashrc and .profile is read, and when. Understand that and it is solved.This article answers these questions:
- What is the difference between
.bashrcand.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.
.bashrcand.profileare 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.
.bashrc and .profile have similar names. I cannot tell which one to write in.| 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.
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:
- Connecting to a server via SSH (
ssh user@host) - Switching to rootThe special administrator account allowed to do anything on the system. with
sudo -i - Switching users with
su -
Files loaded:
/etc/profile~/.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:
/etc/bash.bashrc~/.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.
.bashrc aliases are missing over SSH. Is that because the login shell never reads .bashrc?.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
.bashrc. Even so, after SSH-ing in I get "command not found"..bashrc..bash_profile that loads .bashrc. Write it once and it happens automatically from then on.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~/.profilefrom being read, and thePATHUbuntu 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
fiThose 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
.bashrc and saved it. My open terminal still cannot use the new alias.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.
$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.
| 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.
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
.bashrc when you open a terminal, .profile when you log in. Keep those two apart in your head.source. Got it.echo $0 to see which kind of shell you are in.Summary
Key Points to Remember
~/.bashrcis loaded when you open a terminal (interactive shell)~/.profileis loaded on SSH login or system login (login shell)~/.bash_profiletakes priority over~/.profilewhen both exist- To use
.bashrcsettings over SSH, call it from~/.profileor~/.bash_profile - To apply edits immediately, run
source ~/.bashrc
Today's 3-Line Summary
~/.bashrcis read when you open a terminal;~/.profileis read when you log in- When aliases fail over SSH, call
~/.bashrcfrom~/.profile - To reach a terminal that is already open, run
source ~/.bashrc