.bashrc vs .bash_profile
Learning Before Doing
I thought it would make more sense to learn what the difference between .bashrc and .bash_profile was before making any edits to them. But learning what the difference between the two was required me to learn more about shells first.
TLDR
The difference between the two files is that .bashrc is run for non-login shells whereas .bash_profile is run for login shells.
Interactive vs. Non-Interactive Shells
Interactive shells are shells that wait for the user to input commands. For example, when I ran the wsl -d Ubuntu command, that was acting through an interactive shell since it was waiting for my inputs.
On the other hand, a non-interactive shell is a shell that is used for running scripts or commands without user input. For example, when running a Python file by writing python file_name.py, the command is being run by a non-interactive shell after inputting that command, as there is no further user input required after executing that command.
If the process needs to be terminated, the user can use Ctrl + C to stop it.
Login vs Non-Login Shells
Login shells are the shells that are run when a user logs in, either locally or remotely. For example, when I ran the wsl -d Ubuntu command, this starts a login shell in the Ubuntu instance. Other examples of when login shells would be invoked include using SSH to access a computer.
Non-login shells are the shells that are run when a user starts a new shell session after logging in. This usually happens by starting a new terminal/session or launching a shell from an existing one.
To see which type of shell is in use are use, a user can write the following command into Bash
If what is returned begins with a -, then the shell session is in a login shell. As the example below shows, I am in a login shell because the console shows -bash.
To switch it to a non-login shell, I can simply start a new bash instance by invoking the bash command.
Since running echo $0 returns bash (without a prefixed dash), I am now in a non-login shell.
Why Does this Matter?
This matters because the main topic of this page, .bashrc vs .bash_profile are directly related to these concepts.
.bashrc
.bashrc is the file that is executed by Bash whenever a non-login shell is started.
.bash_profile
.bash_profile is the file that is run by Bash whenever a login shell is started (if it exists).
.profile
There is also a third file called .profile. It runs if .bash_profile doesn't have any contents or doesn't exist. It is also run whenever a login shell is started.
I encoutered .profile because I was curious why the .bashrc was being run even when I was using a login shell. Turns out that the .profile file on the Ubuntu distro I had installed will run .bashrc by default.