Simple inquiry: I have just realized that I have never seen a shebang on top of a .bashrc
script, which leads me to think the system uses the default shell to source it upon login (${SHELL}
). I am pondering over reasons why that is the case, i.e. is it considered a bad habit to use something other than the default shell to run the login script.
Shell – Why No Shebang in .bashrc/.bash_profile?
bashrcprofileshebangshell
Best Answer
.bashrc
and.bash_profile
are NOT scripts. They're configuration file which get sourced every timebash
is executed in one of 2 ways:The INVOCATION section of the bash man page is what's relevent.
You can control when they get loaded through the command line switches,
--norc
and--noprofile
. You can also override the location of where they get loaded from using the--rcfile
switch.As other's have mentioned you can mimic how these files get loaded through the use of the
source <file>
command or the use of the. <file>
command.It's best to think of this functionality as follows:
Methods for invoking
This topic seems to come up every once in a while, so here's a little cheatsheet of the various ways to invoke
bash
and what they result in. NOTE: To help I've added the messages "sourced $HOME/.bashrc" and "sourced $HOME/.bash_profile" to their respective files.basic calls
bash -i
bash -l
bash -il -or- bash -li
bash -c "..cmd.."
NOTE: Notice that the
-c
switch didn't source either file!disabling config files from being read
bash --norc
bash --noprofile
bash --norc -i
bash --norc -l
bash --noprofile -i
bash --noprofile -l
bash --norc -i -or- bash --norc -l
More esoteric ways to call bash
bash --rcfile $HOME/.bashrc
bash --norc --rcfile $HOME/.bashrc
These failed
bash -i -rcfile ~/.bashrc
bash -i -rcfile .bashrc
There are probably more but you get the point, hopefully....
What else?
Lastly if you're so enthralled with this topic that you'd like to read/explore more on it, I highly suggest taking a look at the Bash Beginners Guide, specifically section: 1.2. Advantages of the Bourne Again SHell. The various subsections under that one, "1.2.2.1. Invocation" through "1.2.2.3.3. Interactive shell behavior" explain the low level differences between the various ways you can invoke
bash
.