Unix Introduction — Bash Startup Files Loading Order

김영석
3 min readApr 15, 2018

When you run an application, particularly if it is a java application, you definitely need to have compatible versions of JDK that comes with JRE which is inclusive of Java Virtual Machine. Furthermore, what’s more important to actually execute a java program is setting Environment Variable such as PATH, CLASSPATH, JAVA_HOME. From my experience, especially when I started learning Java Programming Language, I went through a lot of errors caused by inappropriate settings of those variables. On top of that, Unix Operating System is quite different from Windows in terms of setting environment variables and managing them.

When it comes to setting some environment variables, I have to stumble across this agony that to what loading files should I put environment variables since Unix has a couple of startup files that look so similar in their name like /etc/profile, ~/.bash_profile, ~/.bash_login, ~/.profile. Thus, here I would like to clarify which startup file goes first and last in the sequence of execution .

Fortunately, some nice guy have already summarized how the sequence goes behind the scene for the Bash Shell.

This is excerpt from https://shreevatsa.wordpress.com/2008/03/30/zshbash-startup-files-loading-order-bashrc-zshrc-etc/

As you can see, shells could be simply divided into three types as below: interactive login, interactive non-login, and just simple script that runs withou t any interaction with user action.

(Follow this link for further info about shells: Unix Introduction — Shell)

Basically, the alphabetical order means who is going to be executed first and then what goes next. A goes first, B goes next and then C for each column.

When it comes to B1, B2, B3 for the interactive login, the only first one that gets found will be only executed.

+----------------+-----------+-----------+------+
| |Interactive|Interactive|Script|
| |login |non-login | |
+----------------+-----------+-----------+------+
|/etc/profile | A | | |
+----------------+-----------+-----------+------+
|/etc/bash.bashrc| | A | |
+----------------+-----------+-----------+------+
|~/.bashrc | | B | |
+----------------+-----------+-----------+------+
|~/.bash_profile | B1 | | |
+----------------+-----------+-----------+------+
|~/.bash_login | B2 | | |
+----------------+-----------+-----------+------+
|~/.profile | B3 | | |
+----------------+-----------+-----------+------+
|BASH_ENV | | | A |
+----------------+-----------+-----------+------+
| | | | |
+----------------+-----------+-----------+------+
| | | | |
+----------------+-----------+-----------+------+
|~/.bash_logout | C | | |
+----------------+-----------+-----------+------+

For better understanding of who the flow works, you can refer to this chart.

Since I am using Mac OS X, I belong to the Interactive Login Shell category. Bash will look up startup files at /etc/profile first and then find any startup file that comes first among ~/.bash_profile, ~/.bash_login, ~/.profile. If none of this process seems unnecessary, you may include all startup settings in ~/.bashrc.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

김영석
김영석

Written by 김영석

I love problem solving and hate repetition of tedious tasks. I like automating, streamlining, optimizing, things.

No responses yet

Write a response