Shell – Why No Shebang in .bashrc/.bash_profile?

bashrcprofileshebangshell

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.

Best Answer

.bashrc and .bash_profile are NOT scripts. They're configuration file which get sourced every time bash is executed in one of 2 ways:

  • interactive
  • login

The INVOCATION section of the bash man page is what's relevent.

A login shell is one whose first character of argument zero is a -, or one started with the --login option.

An interactive shell is one started without non-option arguments and without the -c option whose standard input and error are both connected to terminals (as determined by isatty(3)), or one started with the -i option. PS1 is set and $- includes i if bash is interactive, allowing a shell script or a startup file to test this state.

The following paragraphs describe how bash executes its startup files. If any of the files exist but cannot be read, bash reports an error. Tildes are expanded in file names as described below under Tilde Expansion in the EXPANSION section.

When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. The --noprofile option may be used when the shell is started to inhibit this behavior.

When a login shell exits, bash reads and executes commands from the file ~/.bash_logout, if it exists.

When an interactive shell that is not a login shell is started, bash reads and executes commands from ~/.bashrc, if that file exists. This may be inhibited by using the --norc option. The --rcfile file option will force bash to read and execute commands from file instead of ~/.bashrc.

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:

  1. bash starts up with a bare environment
  2. bash then opens one of these files (depending on how it was invoked as interactive or login, and then...
  3. ...line by line executes each of the commands within the file...
  4. when complete gives control to in the form of a prompt, waiting for input

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

  1. bash -i

    $ bash -i
    sourced /home/saml/.bashrc
    
  2. bash -l

    $ bash -l
    sourced /home/saml/.bashrc
    sourced /home/saml/.bash_profile
    
  3. bash -il -or- bash -li

    $ bash -il
    sourced /home/saml/.bashrc
    sourced /home/saml/.bash_profile
    
  4. bash -c "..cmd.."

    $ bash -c 'echo hi'
    hi
    

    NOTE: Notice that the -c switch didn't source either file!

disabling config files from being read

  1. bash --norc

    $ bash --norc
    bash-4.1$ 
    
  2. bash --noprofile

    $ bash --noprofile
    sourced /home/saml/.bashrc
    
  3. bash --norc -i

    $ bash --norc -i
    bash-4.1$ 
    
  4. bash --norc -l

    $ bash --norc -l
    sourced /home/saml/.bashrc
    sourced /home/saml/.bash_profile
    
  5. bash --noprofile -i

    $ bash --noprofile -i
    sourced /home/saml/.bashrc
    
  6. bash --noprofile -l

    $ bash --noprofile -l
    bash-4.1$ 
    
  7. bash --norc -i -or- bash --norc -l

    $ bash --norc -c 'echo hi'
    hi
    

More esoteric ways to call bash

  1. bash --rcfile $HOME/.bashrc

    $ bash -rcfile ~/.bashrc 
    sourced /home/saml/.bashrc
    
  2. bash --norc --rcfile $HOME/.bashrc

    $ bash --norc -rcfile ~/.bashrc 
    bash-4.1$ 
    

These failed

  1. bash -i -rcfile ~/.bashrc

    $ bash -i -rcfile ~/.bashrc 
    sourced /home/saml/.bashrc
    sourced /home/saml/.bash_profile
    bash: /home/saml/.bashrc: restricted: cannot specify `/' in command names
    
  2. bash -i -rcfile .bashrc

    $ bash -i -rcfile .bashrc
    sourced /home/saml/.bashrc
    sourced /home/saml/.bash_profile
    bash: .bashrc: command not found
    

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.

Related Question