Getting to Understand Linux Shell(s), start-up scripts and the environment’s PATH variable
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 —
/etc/bash.bashrc, etc. — and what they do, this is for you.
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 :
The following few of many files affect the user specific settings:
placeholderin case of other shells)
TYPES A SHELL CAN ASSUME
- 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.
- 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.
- Interactive Shell: This kind of shell allows user to enter commands and displays output. Eg, bash running inside terminal
- 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
echo "<filename>" in the following files
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:
/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
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
~/.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