bashrc vs bash_profile - What are the differences?
So, if you are familiar with any Unix-like system you might get found one
of those files at least once in your life - mostly to tweak the
variable, or perhaps to add some
If not, fear not! Today we'll check them very closely and see what are their
purpose, what makes them different and how to use them correctly!
So let's start...
Start up scripts
At initialization the
will execute a myriad of files.
Differently from the normal bash scripts, those files don't need to have
the executable bit set and don't need to have the shebang
The order of execution is well defined and is like the follow:
For interactive login shell:
1 - /etc/profile
2 - ~/.bash_profile
The step #1 can fail if the files doesn't exists (but isn't an error).
The step #2 will only execute
the first file that was found.
For interactive non-login shell:
1 - /etc/bash.bashrc
2 - ~/.bashrc
Both steps can fail if the file isn't found, but none of them are errors.
So we just saw that
is executed when we have
an interactive LOGIN shell
when we have an interactive NON-LOGIN shell
But what that means in reality?
Interactive LOGIN shell:
As the name states is the "type"
of shell that is spawned when we (might)
need to enter our credentials
at usage. As examples we might say when you first login into your machine, or
perhaps when you login via
into a it.
Here everything that inside
will be executed, but not
the other start up scripts.
Interactive NON-LOGIN shell:
Is the "type"
of shell that
is created when we don't need use our credentials to use it. The first example
that comes to mind is when you open a new
As you might expect, on those situations the
be executed, but not any of the other files.
Oh my dear macOS!
If you try to create a
file on macOS and test
the initialization order that we learned before we'll see that this isn't
working correctly at all. Every time the Terminal.app
seems to execute
What is going on is that, for some reason, Apple decided that the Terminal.app
will by default spawn interactive login shell
every single time.
So like we already know, only the bash_profile
will be executed.
3- bash manpage