.bash_profile are NOT scripts. They're configuration file which get sourced every time
bash is executed in one of 2 ways:
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
one started with the
An interactive shell is one started without non-option arguments and
-c option whose standard input and error are both
connected to terminals (as determined by
isatty(3)), or one started
-i option. PS1 is set and
interactive, allowing a shell script or a startup file to test this
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
~/.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
~/.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
option will force bash to read and execute commands from file instead
You can control when they get loaded through the command line switches,
--noprofile. You can also override the location of where they get loaded from using the
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:
- bash starts up with a bare environment
- bash then opens one of these files (depending on how it was invoked as interactive or login, and then...
- ...line by line executes each of the commands within the file...
- 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.
$ bash -i
$ bash -l
bash -il -or- bash -li
$ bash -il
bash -c "..cmd.."
$ bash -c 'echo hi'
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 -i
bash --norc -l
$ bash --norc -l
bash --noprofile -i
$ bash --noprofile -i
bash --noprofile -l
$ bash --noprofile -l
bash --norc -i -or- bash --norc -l
$ bash --norc -c 'echo hi'
More esoteric ways to call bash
bash --rcfile $HOME/.bashrc
$ bash -rcfile ~/.bashrc
bash --norc --rcfile $HOME/.bashrc
$ bash --norc -rcfile ~/.bashrc
bash -i -rcfile ~/.bashrc
$ bash -i -rcfile ~/.bashrc
bash: /home/saml/.bashrc: restricted: cannot specify `/' in command names
bash -i -rcfile .bashrc
$ bash -i -rcfile .bashrc
bash: .bashrc: command not found
There are probably more but you get the point, hopefully....
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, "18.104.22.168. Invocation" through "22.214.171.124.3. Interactive shell behavior" explain the low level differences between the various ways you can invoke