What is the login shell?

The login shells' responsibility is to start the non-login shell and to make sure that your environment variables are set so as to ensure that you can get all the default parameters needed at start-up.

Your login shell will set the PATH environment variable, TERM, the UID and GID of the terminal amongst other things. These are the essential requirements in order to work efficiently. Environmental variables will be covered in detail later. Additionally, the login-shell will set default variable such as USERNAME, HISTSIZE, HOSTNAME, HOME, etc.

Upon start-up, your login shell consults two sets of files:

  1. users', as well as the system-wide login shell initialisation files also known as the profile files

  2. users', as well as the system-wide non-login shell initialisation files commonly referred to as 'shell rc' files.

System-wide profile and shell rc initialisation files reside in the /etc directory, and are owned by root.

System-wide initialisation and profile files:

/etc/profile 
/etc/bashrc
            

Users' profile and shell rc files are owned by the specific user, reside in their home directories and are hidden. [2]

~/.bash_profile
~/.bashrc
            

The profile files contain the initialisation data used at login time, thus:

/etc/profile 
~/.bash_profile
            

are used by the login shell.

The non-login shell (bash in my case) files are:

/etc/bashrc
~/.bashrc
            

which are run in order to set up variables and other necessaries at shell initialisation.

There are many things you can achieve using these files. For example you can initialise parameters, you can set your PATH, you can set what your prompt looks like, and much more.

Using these files, you can set up your entire environment. Obviously because you, are the owner of your ~/.bash_profile and ~/.bashrc, you have full control to make changes to these files.

Only the root user can change the /etc/profile and /etc/bashrc.

The appendix Appendix C details the differences between bash, tcsh and ksh.

SUMMARY: At login time, your login shell consults /etc/profile which is owned by root, your home ~/.bash_profile which is owned by yourself, the /etc/bashrc which is owned by root and your home ~/.bashrc which is owned by yourself.

Each time a new shell is started, it executes the /etc/bashrc and ~/.bashrc.

Notice that starting a new shell without logging out and in again (a child process) means that the shell has no need to run the profile files again.

Figure 2.1. Parent- and sub-shells

Parent- and sub-shells

At your command prompt type:

bash
            

This will start a second shell. Looking at the diagram you can see that from the original shell, we've started a new shell. Is that original shell gone? Absolutely not. All you have done is to run a command that just happens to be a new shell. The original shell is still running and so is the new shell.

Again we could run bash at the prompt which would start yet another shell. Each time we run bash, it's consulting /etc/bashrc and ~/.bashrc using these files to initialising the shell. So how do we get back to our original shell? Well we could type:

exit
            

This will return us to the previous shell. Within this shell, we can type exit again which will return us to our original shell. If we type exit one more time, it will return us to our login prompt.

Understanding the distinction between your profile files and your shell rc files is important because when you start to program in the shell you need to know where to set variables in order that they be propagated from shell to shell correctly.

[Note] Note

A common thread in Linux is that initialisation files are frequently named by including an 'rc' in the name. The editor, improved vi or vim, uses an initialisation file called .vimrc. The run level initialisation directories are called rc0, rc1, rc2, etc. Hence the name shell rc files, since it's bashrc (for bash) or kshrc (for ksh) of cshrc (for tcsh)

Exercises

These exercises should assist in your understanding of the login and non-login shell initialisation files.

  1. Log in as a user, edit your .bash_profile. Add a variable called MYFULLNAME as follows:

    MYFULLNAME="Hamish B Whittal"

  2. Save the file and logout.

  3. Now login again.

  4. Type the following at the prompt:

    echo $MYFULLNAME

    What happens? Why?

  5. Now type bash again. In the new shell that open type the following:

    echo $MYFULLNAME

    What happens? Why?

  6. Start another "bash" shell

    In it type;

    echo $MYFULLNAME

    What happens? Why?

  7. Edit your .bashrc, adding this:

    MY_FAV_MOVIE="Finding Nemo"
  8. Save the file and log out, log in again then type the following at your prompt:

    echo $MY_FAV_MOVIE
    echo $MYFULLNAME
                            

    What happens? Why?

  9. Type bash;

    echo $MY_FAV_MOVIE
    echo $MYFULLNAME
                            

    What happens? Why?

  10. Type bash;

    echo $MY_FAV_MOVIE
    echo $MYFULLNAME
                            

    What happens? Why?

  11. Can you explain what is happening in these examples and why?

  12. Exit back to your original prompt.



[2] ~(a tilda) is a shortened means of referring to a users' home directory