The purpose of .bashrc is to serve as a personal configuration script for the Bash shell.
Whenever you open a new terminal window or log into your system using the Bash shell, this file is executed automatically. It ensures that your environment is set up exactly how you like it.
Here are the four primary roles it plays:
1. Defining "Permanent" Environment Variables
As you saw in the manual steps, if you export a variable in the terminal, it disappears when you close that window. By putting your export commands inside .bashrc, you ensure they are recreated every time you start a new session.
- Example: Setting your
PATHso the system always knows where your custom scripts are located.
2. Creating Aliases (Shortcuts)
.bashrc is the best place to store "aliases"—short commands that stand in for longer ones to save you time.
- Example: Adding
alias ll='ls -la'to your.bashrcallows you to just typellto see a detailed list of all files.
3. Customizing the Command Prompt (PS1)
Have you ever seen a terminal that shows the time, the current git branch, or has different colors? That is controlled by a variable called PS1 inside the .bashrc file. It allows you to change the look and feel of your command line.
4. Setting Shell Options
You can change how the shell behaves. For example, you can tell Bash to ignore duplicate commands in your history or to automatically correct small typos in directory names when using the cd command.
The "Morning Routine" Analogy
Think of .bashrc as a Morning Routine Checklist:
- If you don't have a routine, every morning you have to remember: "Where is my coffee? What is my schedule? Where are my keys?" (This is like manually typing
exportevery time). - The
.bashrcis a script that runs the moment you wake up (start the shell). It automatically puts the coffee in your hand, opens your calendar, and puts your keys in your pocket. You don't have to think about it; it’s just ready for you.
Note for this Lab: Since this environment uses Zsh, the file performing this "Morning Routine" is .zshrc instead of .bashrc. They do the exact same job, just for different shells!