- Posted on
- • commands
How to Use `bashrc` and `bash_profile`
- Author
-
-
- User
- Linux Bash
- Posts by this author
- Posts by this author
-
Understanding and Using .bashrc
and .bash_profile
Whether you're a seasoned developer or just beginning to explore the Linux environment, mastering the configuration of your shell using files like .bashrc
and .bash_profile
can significantly enhance your command line efficiency and customization. This article covers what these files are, when each one is used, and how to effectively use them for setting up your Linux environment.
What Are .bashrc
and .bash_profile
?
On Unix-like operating systems, including Linux and macOS, Bash (short for Bourne Again SHell) is one of the most commonly used shells. It comes with configuration files that initialize shell environments each time a terminal window is opened or a new login occurs.
.bashrc
: The "rc" in.bashrc
stands for "run command." This file is read and executed by Bash every time a new interactive non-login shell is started. Therefore, it’s primarily used for configuring shell settings related to the working session, like setting up or enabling aliases, functions, shell prompts, and other utilities that you wish to make available in every terminal session..bash_profile
: On the other hand,.bash_profile
or sometimes calledprofile
, is read and executed when Bash is invoked as an interactive login shell (e.g., when you log into your system or invoke bash with the--login
option). This file is ideal for actions that need to be set up once per login session, such as setting environment variables, performing tasks that need to run only once during user login, like starting graphical environments or command line tools such as tmux or screen.
When to Use .bashrc
vs .bash_profile
?
Use
.bashrc
for commands that should occur in every interactive non-login shell. This covers things like setting PATH variables incrementally, defining aliases and functions used at the terminal.Use
.bash_profile
for commands that should run once at the login. This is suitable for initializing environment variables likePATH
,EDITOR
, orNODE_ENV
, and beginning other login-only dependent processes.
Setting Up and Configuring .bashrc
and .bash_profile
1. Editing .bashrc
You can find this file in your home directory (~/.bashrc
). If it doesn’t exist, you can create it. Here’s how you can set an alias to make navigation easier:
# Open .bashrc with an editor, here using nano
nano ~/.bashrc
# Add an alias
echo "alias ll='ls -la'" >> ~/.bashrc
After adding your configurations, reload the file:
source ~/.bashrc
This will enable the newly added command.
2. Editing .bash_profile
Similarly, this file can be created if it doesn’t exist in your home directory. To add environment variables:
nano ~/.bash_profile
# Set ENV VAR
echo "export PATH=\"$PATH:/home/user/my_scripts\"" >> ~/.bash_profile
echo "export EDITOR='nano'" >> ~/.bash_profile
Load the new settings by sourcing the file:
source ~/.bash_profile
Best Practices
Always remember to source the file after editing to apply changes.
For consistency and to ensure that terminal customizations and environment variables are available in all types of shells, you can source
.bashrc
within your.bash_profile
:if [ -f ~/.bashrc ]; then source ~/.bashrc fi
Keep your configuration scripts organized and comment on more complicated setups or uncommon configurations.
Conclusion
Understanding how to properly use .bashrc
and .bash_profile
will help you create a more responsive and tailored working environment. These files give you control over the behavior of your terminal sessions and can help you automate setup processes and increase productivity. Whether it's customizing the shell prompt, setting paths, or defining aliases, mastering these files is a crucial skill for every Linux and macOS user.
Further Reading
DigitalOcean Introduction to
.bashrc
and.bash_profile
A guide explaining their roles in system configurations and usage scenarios.
Read moreLinuxize on Bash
.bashrc
and.bash_profile
Files
Offers detailed insights on setting and managing these configuration files effectively.
Explore furtherIBM Developer: Detailed Overview of
.bashrc
Includes tips on optimizing shell interaction through effective.bashrc
configurations.
View the guideArchWiki’s Guide to Using Bash Configuration Files
Community-curated insights and advanced configurations tailored for Arch Linux users.
Read more hereMakeUseOf Explanation on Bash Profiles
Simplifies how to leverage.bash_profile
for improving your login sessions.
Click to read