bashrc vs. bash_profile: What Is the Difference?
Last Updated :
29 Jan, 2024
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 editorStep 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 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".
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 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";
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.
Now you can see the welcome message.Main difference between ".bashrc" and ".bash_profile"
|
Runs when the terminal is opened.
| Runs only when logging into the system using the shell (e.g., SSH login).
|
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.
|
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.
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
Linux Commands Cheat Sheet Linux, often associated with being a complex operating system primarily used by developers, may not necessarily fit that description entirely. While it can initially appear challenging for beginners, once you immerse yourself in the Linux world, you may find it difficult to return to your previous W
13 min read
CTE in SQL In SQL, a Common Table Expression (CTE) is an essential tool for simplifying complex queries and making them more readable. By defining temporary result sets that can be referenced multiple times, a CTE in SQL allows developers to break down complicated logic into manageable parts. CTEs help with hi
6 min read
What is Vacuum Circuit Breaker? A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac
13 min read
Linux/Unix Tutorial Linux is one of the most widely used open-source operating systems. It's fast, secure, stable, and powers everything from smartphones and servers to cloud platforms and IoT devices. Linux is especially popular among developers, system administrators, and DevOps professionals.Linux is:A Unix-like OS
10 min read