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 this post

10 thoughts on “Difference between login shell and non-login shell

  1. @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.

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

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

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

Post Comment