91

It is said that settings for non login shell to go into .bashrc file and login shell settings to go into .profile file.

What is really meant by login and non-login shells?

Please explain without using technical jargon as far as possible.

2 Answers 2

98

Simply put:

  • If you open a shell or terminal (or switch to one), and it asks you to log in (Username? Password?) before it gives you a prompt, it's a login shell.
  • If it doesn't (like gnome-terminal), and lets you use it straight away, it's a non-login shell.

If you are a normal user of Ubuntu Desktop, the only login shell is...your desktop (you type a password to get in, right ;)? Well, technically it's a login shell that starts a GUI, but that's getting in to jargon. And yes, it will read the settings in .profile

The only time you (a normal user) will probably see a login shell that looks like a login shell is if you are having some problem with your desktop and you switch to a virtual terminal with the Ctrl+Alt+F1 shortcut.


The other general cases for having a login shell include:

  • accessing your computer remotely via ssh (or connecting locally with ssh localhost)
  • simulating an initial login shell with bash -l (or sh -l)
  • simulating an initial root login shell with sudo -i
    • or sudo -u username -i for another non-root user
  • authenticating as another non-root user with su - username (and their password)
  • using the sudo login command to switch user
8
  • If I start my Eclipse IDE from terminal, it opens as expected, but if I try to open it by clicking the Eclipse icon, it is unable to recognize Java location (unless otherwise PATH for Java is set in .profile file). That means clicking Eclipse icon needs a login shell, why?
    – DUKE
    Jun 26, 2012 at 12:45
  • 2
    @DUKE, No, it means that environment variables need to be set differently when you are using a desktop/GUI versus a true command-line-only console system. Put your PATH, etc. in ~/.pam_environment (variables only, no bash commands in there!), logout, login, and watch everything magically appear in the desktop as well as in gnome-terminal!
    – ish
    Jun 26, 2012 at 12:49
  • 1
    Login via ssh does not invoke a login shell. It doesn't load /etc/profile, /etc/profile.d or ~/.profile.
    – xuhdev
    Jul 19, 2014 at 0:17
  • See this question.
    – xuhdev
    Jul 19, 2014 at 0:29
  • @xuhdev Login via ssh does invoke a login shell and it does load /etc/profile, /etc/profile.d and ~/.bash_profile.
    – MichaelZ
    Apr 15, 2015 at 14:04
11

I do not think that correct answer can be given without “technical jargon”. Since this question is the first one popping up in Google for the query “what is a login shell” I am providing a more correct answer below:

Login shell is simply a shell that was told to be a login shell. It does not mean shell that pops up after you login, though usually application that logs you in is telling shell it launches to be a login shell. There exists the following ways to tell shell it should be a login one:

  1. Running shell with -l or --login argument assuming it knows it (I do not know any shells which do not know -l, but --login is only supported by a few shells).
  2. Running shell with argv[0] set to -{some_string} (i.e. with HYPHEN-MINUS prepended to usual argv[0] or to some other string). This is what ssh and su do: su just runs executable with -su as argv[0] (hello to everybody thinking argv[0] has something to do with currently running executable name), ssh runs zsh with -zsh when user has set /bin/zsh as his shell.

Loginess of the shell has absolutely nothing to do with anybody asking you a password or performing some other authenication procedure. Some programs like ssh or login (or some terminal emulators like urxvt) run shells as a login ones using argv[0] that starts with a HYPHEN-MINUS. Some like su or sudo (or zsh: see - precommand modifier described in section PRECOMMAND MODIFIERS in man zshmisc) do not do this by default, but can be told so. Some have the only option of telling shell to be the login one using its argument (i.e. bash -l): ssh with a command argument (that explicitly tells ssh what to run on the remote end).

Generally it is better to first consult the documentation of the program used to invoke the shell to determine whether shell will be the login one and second perform some tests to determine whether app will launch a login shell (e.g. by adding echo to .profile).

5
  • I'm interested in the second option: how may I actually change argv[0] before (or after) invoking bash? Can it be done from the command line? Mar 23, 2016 at 10:44
  • @VeryHardCoder This depends on the application you use to run the command. In C code it is done by directly supplying argv[0] to one of the exec* functions, natural and inevitable: you always supply both argv[0] and path to command actually run when using exec* functions, even if you do not ever want argv[0] to be different from command run. Other languages provide their own ways. Specifically bash allows using exec -a new_argv0 bash, but this, of course, will replace currently running shell with whatever you execed, so you may need to use subshell ((exec -a -zsh zsh))
    – ZyX
    Mar 23, 2016 at 18:47
  • Ok, got it! Actually, it could be done even in a simpler way: (exec -l bash)... Mar 24, 2016 at 15:08
  • 3
    What is the functional difference between the two? Is this simply a boolean flag?
    – Alexey
    Oct 9, 2016 at 13:00
  • 1
    @Alexey Main functional difference is a set of configuration files used at startup. $0, and, maybe also something else (variable, setting, etc) is set so that loginess of the shell may be detected in the configuration file, but who bothers actually detecting this so it will make any difference? That’s all I know.
    – ZyX
    Oct 9, 2016 at 20:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .