The Linux .bashrc and .bash_profile Files
This post explains the difference between .bashrc and .bash_profile in Linux, covering when Bash reads each file, what settings belong in each one, and how to apply changes without restarting your terminal. It is aimed at beginners who want to understand and customize their Bash shell environment.
When you start working in the Linux terminal, you will quickly notice that your shell behaves in certain ways by default: specific colors, custom prompts, handy aliases, and environment variables that just seem to be there. These settings live somewhere, and that somewhere is almost always one of two hidden files in your home directory: .bashrc and .bash_profile. Understanding the difference between them is one of those fundamentals that saves you a lot of head-scratching later.
What Are These Files?
Both .bashrc and .bash_profile are shell configuration files. They are plain text files that Bash reads and executes automatically when you start a new shell session. Think of them as startup scripts that run in the background before you type a single command. Because their names begin with a dot, they are hidden by default. You need to run ls -a ~ to see them listed in your home directory.
The Key Difference: Login vs. Interactive Shells
This is the core concept. Bash distinguishes between two types of shell sessions:
- Login shell: A session where you authenticate with a username and password. Examples include SSH sessions, TTY console logins, and some desktop environments on first login. Bash reads the first file it finds among
.bash_profile,.bash_login, and.profile, in that order. Only one of these files is read per login session. - Interactive non-login shell: A session you open after you are already logged in. A new terminal window in Ubuntu, a tab in your terminal emulator, or running
bashfrom within another shell. Bash reads.bashrcfor these sessions.
In practice, this means .bash_profile is read once when you log in, and .bashrc is read every time you open a new terminal window.
What Goes in Each File?
.bash_profile
This file is the right place for settings that should only run once per login session. Common examples include setting environment variables that need to be inherited by child processes. It is also standard practice to source .bashrc from within .bash_profile, ensuring that your interactive shell settings apply in login shells as well. Without this, aliases and prompt customizations defined in .bashrc would not be available in login shell sessions.
# ~/.bash_profile
# Set environment variables for the login session
export JAVA_HOME="/usr/lib/jvm/java-17-openjdk-amd64"
export PATH="$HOME/bin:$PATH"
# Source .bashrc so login shells get interactive settings too
if [ -f ~/.bashrc ]; then
source ~/.bashrc
fi.bashrc
This is where most of your day-to-day customizations live. Since it runs every time you open a terminal, it is the right home for aliases, functions, prompt customization, and interactive shell behavior.
# ~/.bashrc
# Custom aliases
alias ll='ls -lah --color=auto'
alias gs='git status'
alias ..='cd ..'
# Custom prompt: show user, host, and current directory
PS1='\u@\h:\w\$ '
# Enable case-insensitive tab completion
bind 'set completion-ignore-case on'Applying Changes Without Restarting
After editing either file, the changes do not take effect automatically in your current session. You have two options:
- Source the file: Run
source ~/.bashrcor the shorthand. ~/.bashrcto reload it immediately in the current session. - Open a new terminal: Any new terminal window will pick up the latest version of the file automatically.
A Quick Tip for Ubuntu Users
On Ubuntu and many Debian-based systems, a default .bashrc is already created for you when your user account is set up. It includes sensible defaults like enabling color support and a few aliases. It is worth reading through it once so you understand what is already configured before you start adding your own settings.
To view the file, run:
cat ~/.bashrcWhat's Next?
Now that you understand how Bash loads its configuration, the next logical step is exploring environment variables in more depth. Knowing how to set, export, and inspect variables like PATH, HOME, and custom ones you define yourself gives you real control over how your shell and programs behave. We will cover that in the next post in this series.