Getting to Understand Linux Shell(s), start-up scripts and the environment’s PATH variable

Deepak Aggarwal
Apr 11, 2018 · 3 min read

If you have ever put something in a file like .bashrc and had it not work, or are confused by why there are so many different files — .bashrc, .profile, /etc/profile, /etc/bash.bashrc, etc. — and what they do, this is for you.

Image for post
Image for post
Analogy between a maze and the shell startup files

There are two types of settings that any OS manages:

  • System wide : Common to all users
  • User specific

The following few of many files affect the system wide settings :

  • /etc/profile
  • /etc/environment

The following few of many files affect the user specific settings:

  • ~/,bashrc or ~/.zshrc or ~/.<shell>rc (<shell>is a placeholder in case of other shells)
  • ~/.profile
  1. Login Shell : That require a user to login in before using the shell. To exit such a shell Ctrl + Alt + F7 . To login such a shell, Ctrl + Alt + F2 . When you login to you user account, you are automatically logged into this short of shell. Everything else is a child process of this “super” process.
  2. Non-Login Shell : This does not require to login into the shell. The program (shell) that is started by default in your terminal is one such example.
  3. Interactive Shell: This kind of shell allows user to enter commands and displays output. Eg, bash running inside terminal
  4. Non-Interactive Shell: An example include a shell script.

The files that bash will read when launched depend on the type of shell it is running as.

Files read at startup

After spending almost a day to deeply understand the process, I will try to write how I figured it out. The activity will help you realise the process.

Step 1. Understanding the files that are automatically loaded and the order in which they do so

a) Write echo "<filename>" in the following files

  • /etc/profile
  • /etc/profile.d/<anyfile>
  • ~/.bashrc
  • ~/.profile
Image for post
Image for post
First line of each file is an echo command

In case any of the files is missing, those files are skipped.

/etc/profile --> ~/.bash_profile --> ~/.bash_login --> ~/.profile
/etc/bash.bashrc --> ~/.bashrc

Other files that are always read are:

a) /etc/environment This isn’t a script but a simple file that contains variables definitions. Its a system wide file.

b) Most of the time, /etc/profile script contains instructions to load the all the *.sh files located in /etc/profile.d/.

When you run the terminal, you will see the following lines .

Image for post
Image for post

This means that shell loads only ~/.bashrc by default in the interactive non-login mode.

Every other variable in the environment comes from login shell that is automatically invoked when you first logged in to your OS.

To verify this, invoke shell without importing environment from the surroundings.

env --ignore-environment bash

When you invoke the shell in login mode, you will see the following lines .

Image for post
Image for post

Now, since we know the basic default behaviour of the shell, we must understand that ~/.bashrc file is specific to your bash shell.

If a program, say Webstorm wants to read the PATH variable, it will read source PATH from ~/.profile file, i.e. other programs read ~/.profile file only.

So, if you wish to put something that should be available to all programs, put that in ~/.profile

Coding Blocks

Daily Tidbits on Android, Javascript and Machine Learning

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store