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.

  • 1
    There's a reason why it's called bash rc... – Ajedi32 Aug 29 '13 at 19:06

.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'

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

disabling config files from being read

  1. bash --norc

    $ bash --norc
  2. bash --noprofile

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

    $ bash --norc -i
  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
  7. bash --norc -i -or- bash --norc -l

    $ bash --norc -c 'echo 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 

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, " Invocation" through " Interactive shell behavior" explain the low level differences between the various ways you can invoke bash.

| improve this answer | |
  • @amphibient - sorry it got a little out of hand, hopefully people will appreciate its value and not punish it with downvotes. With this here we can now reference it in other answer down the road. 8-). I was contemplating making a table to show this but that would've been crazy 8-). – slm Aug 29 '13 at 16:54
  • Have actually tested these? I once tried to follow what my bash does in squeeze and it didnt quite behave as the manual suggested – Bananguin Aug 29 '13 at 20:47
  • @Bananguin - every one of those commands was run by me and that's the output that was produced below the commands. The only potential "thing" with my setup might be that my .bash_profile includes a line to source the .bashrc. But I believe that to be very typical of setups. – slm Aug 29 '13 at 20:49
  • This could be subjective but I would not say that .bashrc and .bash_profile are not scripts. IMHO they are specific purpose scripts which are implicitly sourced during bash initialization or explicitly when you need to apply their modifications. They do not just configure the bash environment (variables, functions, aliases...) as expected from configuration files. They can do any action as in common scripts. For example they can start various actions like background tasks, write log records, initialize some programs etc. Anyway thank you for the detailed summary! – pabouk Sep 9 '13 at 10:18
  • This answer is even better than any answers here stackoverflow.com/questions/415403/…! – Jacob Tomlinson Jan 22 '15 at 15:57

.bashrc scripts are only run by bash itself. They're not free-standing, and they're not intended to be executed by the system. (In fact, they're generally not marked executable, and, as you say, they don't have a shebang line.)

Such scripts are intended to be sourced, since they generally do things like change environment variables ($PATH, for example), which are expected to persist after the script finishes. So it would really be pointless to try to execute one in a subshell.

| improve this answer | |

In addition to the other replies, note that if you want it, nothing forbids you to put a shebang at the beginning of these configuration files.

That wouldn't hurt shell sourcing them as the shebang will be processed just like a regular comment, i.e. ignored.

That might help editors that use syntax highlighting to figure out what programming language is used in the file.

Note that some editors like vim provide alternative ways like modelines for the latter though. i.e. you can always put mode lines at the end of the ~/.bashrc and ~/.bash_profile like so:

<code in ~/.bashrc>
# vim: ft=sh :
| improve this answer | |
  • 2
    The accepted answer from @slm above is great, but this is what I was looking for, with regard to adding a shebang to the start of my .bash_profile on a recommendation from ShellCheck. – jlucktay Dec 4 '17 at 8:29

I read this anywhere don't know where exactly but it's true

The Bash manual is a bit confusing in this area, but Bash does not eXecute ~/.bash_profile like a shell script. It does read the file and then executes the commands within it (You can do something similar by running source ~/.bash_profile).

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.