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.

Analogy between a maze and the shell startup files

Few terms we get to understand before we can actually start discussing

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

TYPES A SHELL CAN ASSUME

  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
First line of each file is an echo command

Interactive, login shell

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

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

Interactive, Non-login shell

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

Getting things into muscle memory

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

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 .

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

Deepak Aggarwal

Written by

Mentoring people to become better software developers.

Coding Blocks

Daily Tidbits on Android, Javascript and Machine Learning

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade