What is the purpose of .bashrc and how does it work?
.bashrc is a shell script that Bash runs whenever it is started interactively. You can put any command in that file that you could type at the command prompt.
You put commands here to set up the shell for use in your particular environment, or to customize things to your preferences. A common thing to put in .bashrc are aliases that you want to always be available.
.bashrc runs on every interactive shell launch. If you say:
and then hit Ctrl-D three times. bashrc will run three times. But if you say this instead:
then .bashrc won't run at all, since -c makes the Bash call non-interactive. The same is true when you run a shell script
from a file.
Contrast .bash_profile and .profile which are only run at the start of a new login shell. ( bash -l ) You choose whether a command goes in .bashrc vs .bash_profile depending on on whether you want it to run once or for every interactive shell start.
As a counterexample to aliases, which I prefer to put in .bashrc. you want to do PATH adjustments in .bash_profile instead, since these changes are typically not idempotent :
If you put that in .bashrc instead, every time you launched an interactive sub-shell. /some/addition would get tacked on to the end of the PATH again, creating extra work for the shell when you mistype a command.
You get a new interactive Bash shell whenever you shell out of vi with :sh. for example.