Difference between login shell and non-login shell

Understanding the distinction between your shell types, profile files and shell rc files is important because when you apply modifications to your system you need to know where  to set variables in order to make sure that they can be initialized as expected.

Let’s give a quick answer for this topic which covers the exact difference between the two shells.

When you login your system and see the command line prompt, it’s a login shell, and it executes these files in order:

  • /etc/profile
  • ~/.bash_profile
  • ~/.bashrc
  • /etc/bashrc

A non-login shell will only execute the two files in order:

  • /etc/bashrc
  • ~/.bashrc

So what’s a non-login shell?
When issuing this command:

$ ssh joseph@howto.admon.org ‘uptime’

you will gain a non-login shell at the remote side howto.admon.org. when you type bash after login, your new shell is also a non-login shell.

Now, you may have a clear answer to a common system administration question: How can I properly apply a new system variable like JAVA_HOME?

It’s better to set it in /etc/bashrc other than /etc/profile. Addtionally, there’re better choice. If you prefer to make your system clean and tidy, it’s suggested to create single config files in /etc/profile.d, the point is all these file would be initialized by both login shell and non-login shell:

$ grep profile /etc/profile /etc/bashrc
/etc/profile:# /etc/profile
/etc/profile:for i in /etc/profile.d/*.sh ; do
/etc/bashrc:# Environment stuff goes in /etc/profile
/etc/bashrc:        for i in /etc/profile.d/*.sh; do

NOTE: The suffix of the file name should be “.sh”, e.g. /etc/profile.d/java.sh

Further readings: learnlinux.tsf.org.za and linuxquestions.org

Tags: ,


Joseph chen is a system administrator from south China. He has a keen interest in Open Source and system administration solutions.

6 Responses to “Difference between login shell and non-login shell”

  1. Dave says:

    I thought the system-wide profiles are set prior to user profiles, i.e.

    /etc/profile
    /etc/bashrc
    ~/.bash_profile
    ~/.bashrc

  2. [...] The following section would show an example on how to set the variable ORACLE_HOME to be a global one on both Debian and CentOS. Before we start, it’s suggested to have a glance on our former post the difference between login shell and non-login shell. [...]

  3. [...] Here's also a post which should meet your needs quite well, http://planet.admon.org/howto/differ…n-login-shell/ [...]

  4. bellszhu says:

    $ ssh joseph@howto.admon.org ‘uptime’”
    i will gain a login shell at the remote side……

  5. joseph says:

    @bellszhu, It’s indeed a non-login shell, you can change “uptime” to “echo $0″ to identify it, or put some variables in the initial script of login shell for testing.

  6. alex says:

    Good article! Solve my problems with permanent env vars. Thank you!

Leave a Reply

© 2006-2009 Admon Home. All rights reserved.
Powered by Linode Japan