Open In App

bashrc vs. bash_profile: What Is the Difference?

Last Updated : 29 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Let’s clarify that the “.bashrc” and “.bash_profile” files are exclusive to Unix-based operating systems such as Linux and macOS. If you are using the Windows operating system, you won’t find these files on your system. In Linux or macOS, they are located in the “/home/yourusername” directory. The main purpose of both files is to run the scripts written in them. Let’s understand each one individually.

Note: When editing “.bashrc” and “.bash_profile,” always back up the files because mistakes during editing can lead to problems.

What is the use of “.bashrc”?

The purpose of the “.bashrc” file is to determine which commands or scripts should run whenever you open a terminal on your system. For example, suppose you want to execute the “date” command to display the current date and time in the terminal every time you open it. You just have to append the “date” command in the “.bashrc” file. Let’s implement this by following the steps below:

Step 1. Open the terminal and execute the following command:

nano ~/.bashrc” or “code ~/.bashrc (if you prefer using VS Code)
opening-bashrc-in-nano-editor

Opening “.bashrc” in nano editor

Step 2. Append the “date” script at the end of the file content, save the changes and close the “.bashrc” file.

adding-date-script-at-the-end-of-file

Adding “date” script at the end of the file.

Step 3. Execute the following command to apply the changes or just launch the new terminal:

source ~/.bashrc

Step 4. From now on, whenever you open your terminal, the current date and time will be displayed at the top.
“.bashrc” can be used for various purposes like changing to a particular directory, setting aliases, customizing command prompts, terminal looks and more.

What is the use of “.bash_profile”?

The “.bash_profile” file is used for scripting, but these scripts run only once when logging into the system using the terminal. For example, when remotely accessing the system via SSH, scripts from “.bash_profile” will execute. Editing the “.bash_profile” involves the same steps mentioned for the “.bashrc” file. This file is generally utilized for tasks such as setting environment variables, loading configuration files like “.bashrc,” and more.

For example, let’s add a script in “.bash_profile” that prints “Welcome to Remote Computer” whenever we log in to our remote computer.

Step 1: Log in to the remote computer. You will notice there is no message in the terminal like “Welcome to Remote Computer”.

login-into-system

You can see there is no welcome message yet.

Step 2: Edit the “.bash_profile” with the nano editor using the following command:

nano ~/.bash_profile
editing-bash-profile-in-nano

Editing bash profile in using nano editor.

Step 3: Add the script to print the message “Welcome to Remote Computer.” Save the changes and close the file.

echo "Welcome to Remote Computer";
welcome-to-computer-message

Adding sript to print message.

Step 4: From now on, whenever we log in to the remote computer, the message will be printed. Let’s logout and login again to remote computer to see the effect of the script.

logging-into-system-after-addition-of-the-script

Now you can see the welcome message.

Main difference between “.bashrc” and “.bash_profile”

.bashrc

.bash_profile

Execution

Runs when the terminal is opened.

Runs only when logging into the system using the shell (e.g., SSH login).

Use Cases

Typically used for customizing the terminal environment, aliases, and other settings for each terminal session.

Primarily used for setting environment variables and executing scripts that should run once during the login process.

Initialization

Often sourced manually or automatically by the shell when a new terminal session starts.

Sourced automatically during the login process.

Similarities between “.bashrc” and “.bash_profile”

Both files are related to the configuration of the Bash shell in Unix-based operating systems, providing a means to customize the shell environment according to user preferences and requirements. Both files can be edited using any text editor, such as Nano, VS Code, etc.

Conclusion

The “.bashrc” and “.bash_profile” serve complementary roles in configuring the Bash shell. While “.bashrc” is focused on customizing the terminal environment for each session, “bash_profile” is dedicated to tasks that should run only once during the login process. Understanding when and how each file is executed allows users to organize their configurations effectively and tailor the shell environment to their needs.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads