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

Share Button
Tags: ,

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

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


  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://www.admon.org/howto/differ…n-login-shell/ […]

  4. bellszhu says:

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

    i think “$ 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!

  7. Mollie says:

    Its such as you learn my thoughts! You appear to understand a lot approximately this, such as you wrote the guide in it or something. I think that you just can do with a few % to power the message home a little bit, however instead of that, this is wonderful blog. A fantastic read. I’ll certainly be back.

  8. Corazon says:

    I am not sure where you are getting your information, however good topic. I needs to spend some time learning much more or figuring out more. Thank you for magnificent info I used to be on the lookout for this info for my mission.

  9. Chester says:

    Do you have a spam problem on this site; I also am a blogger, and I was wanting to know your situation; many of us have created some nice procedures and we are looking to exchange methods with other folks, please shoot me an email if interested.

  10. joseph says:

    No, we don’t. Thank you all the same.

Leave a Reply

© 2006-2009 Admon Linux. All rights reserved.
Powered by Linode.com