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.
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.
ssh
(or connecting locally with ssh localhost
)bash -l
(or sh -l
)root
login shell with sudo -i
sudo -u username -i
for another non-root
userroot
user with su - username
(and their password)sudo login
command to switch user~/.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!
/etc/profile
, /etc/profile.d
or ~/.profile
.
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:
-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).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
).
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 exec
ed, so you may need to use subshell ((exec -a -zsh zsh)
)
$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.