Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
bashrc vs. bash_profile What Is Difference
If you're new to the command line interface, you may have come across the terms .bashrc and .bash_profile. These are important configuration files found in your home directory that customize your shell environment. However, many users are confused about the differences between these two files. In this article, we'll explore the key differences between .bashrc and .bash_profile and explain how they work.
What is .bashrc?
The .bashrc file is a configuration script for the Bash shell that executes every time you open a new non-login terminal session. This includes opening new terminal windows or tabs in a graphical environment. The file contains commands that customize your interactive shell environment, such as setting aliases, defining functions, and customizing your command prompt.
What is .bash_profile?
The .bash_profile file is executed only during login shells when you log into your system either locally or remotely via SSH. This file runs once per session and is typically used to set environment variables, configure the PATH, and run initialization commands that should execute only at login time.
Key Differences Between .bashrc and .bash_profile
| Aspect | .bashrc | .bash_profile |
|---|---|---|
| Execution Time | Every new non-login shell | Only at login |
| Shell Type | Interactive non-login shells | Login shells |
| Common Usage | Aliases, functions, prompt customization | Environment variables, PATH, startup programs |
| Scope | Current terminal session | Entire login session |
Shell Types Explained
Examples
Example 1: Setting Aliases in .bashrc
Add this line to ~/.bashrc for aliases that work in every new terminal window
alias ll='ls -la' alias grep='grep --color=auto' alias ..='cd ..'
Example 2: Setting PATH in .bash_profile
Add this to ~/.bash_profile to permanently modify your PATH
export PATH="$HOME/bin:$PATH" export PATH="/usr/local/go/bin:$PATH"
Example 3: Custom Prompt in .bashrc
Customize your terminal prompt by adding this to ~/.bashrc
export PS1='\[\e[32m\]\u@\h\[\e[0m\]:\[\e[34m\]\w\[\e[0m\]\$ '
Best Practices
Source .bashrc from .bash_profile
Many systems include this line in .bash_profile to ensure .bashrc is also loaded during login shells
if [ -f ~/.bashrc ]; then
source ~/.bashrc
fi
Reload Configuration Files
After editing these files, reload them without logging out
source ~/.bashrc source ~/.bash_profile
Check Which Files Exist
Different systems may use different file names. Check what exists on your system
ls -la ~/.bash* ~/.profile
Common File Locations and Alternatives
| File | Purpose | Alternative Names |
|---|---|---|
| ~/.bash_profile | Login shell initialization | ~/.bash_login, ~/.profile |
| ~/.bashrc | Non-login shell configuration | Usually just .bashrc |
| /etc/bash.bashrc | System-wide bashrc | /etc/bashrc |
| /etc/profile | System-wide profile | Various in /etc/profile.d/ |
Troubleshooting Tips
Test syntax Use
bash -n ~/.bashrcto check for syntax errors without executing the file.Backup files Always keep backups:
cp ~/.bashrc ~/.bashrc.backupCheck execution Add
echo "Loading .bashrc"to see when files are loaded.Avoid sensitive data Never store passwords or API keys in these files; use environment variables or secure vaults instead.
Conclusion
The key difference is timing: .bash_profile runs once at login for environment setup, while .bashrc runs for every new terminal session for interactive customizations. Understanding this distinction helps you place configurations in the right file and create an efficient, well-organized shell environment.
