What are the differences between a login shell and interactive shell?
Asked Answered
E

2

144

What is a login shell and interactive shell, and what is a .bash_profile and .bashrc?

Elohim answered 12/8, 2013 at 12:25 Comment(2)
See serverfault.com/questions/8882/…Ube
Also see gnu.org/software/bash/manual/html_node/Bash-Startup-Files.htmlUbe
S
155

An interactive shell is one started without non-option arguments, unless -s is specified, without specifying the -c option, and whose input and error output are both connected to terminals (as determined by isatty(3)), or one started with the -i option.

An interactive shell generally reads from and writes to a user’s terminal.

[gnu bash manual]

A login shell is a shell where you login. You can recognize a login shell from a ps -f listing, it will have a hyphen at the start of the program name, for example:

root      3561  3553  0 09:38 pts/0    00:00:00 -bash
qa        7327  3432  0 10:46 pts/1    00:00:00 -bash

An interactive shell is one which reads commands from its standard-input, usually a terminal.

For example,

  • if you login to bash using an xterm or terminal emulator like putty, then the session is both a login shell and an interactive one.
  • if you then type bash then you enter an interactive shell, but it is not a login shell.

If a shell script (a file containing shell commands) is run, then it is neither a login shell nor an interactive one.

Start-up files are highly tailorable in bash:

When a login bash shell is invoked, then /etc/profile is sourced (executed in the current environment). After that, three files are checked for existence. The checks for these files are done in this order, the first one that exists is run.

  • ~/.bash_profile
  • ~/.bash_login
  • ~/.profile

Once a match is found, the other files are ignored, even if they exist. The /etc/bashrc file might be used by both the ~/.bash_profile and the ~/.bashrc files. That would mean that the /etc/bashrc file is sourced on all interactive invocations of bash, whether it is a login or non-login shell.

So, the .bashrc file is also run every time you request a new interactive shell. This does not include a shell script. Normally variables, aliases or functions are placed in this file.

Bash shell scripts read a different file if suitably instructed. If the user defines (usually in their own .bash_profile) a variable BASH_ENV which contains a filename, scripts will read this. If this variable is not set (and exported) then bash scripts will not read any startup files.

Superscribe answered 12/8, 2013 at 12:47 Comment(8)
See man bash at section INVOCATION on the files being loaded on startup, the information there may very likely be clearer.Alic
Is it possible for there to be a login shell that is non-interactive? Sounds impossible.Chris
@CMCDragonkai: yes it is. A non-interactive bash process can be started using the --login (or -l) option. You might do that if you wanted all the startup files to be executed, for example when running from crond. However some scripts make the false assumption that login equates to interactive.Superscribe
I thought cron bash scripts are always non-login and non-interactive? How would you make cron bash scripts login and non-interactive?Chris
@CMCDragonkai: you could execute bash --login from within another bash script which runs under cron.Superscribe
"Once a match is found, the other files are ignored, even if they exist" is incorrect per man bash > INVOCATION, which says bash "first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile"Belva
@mellow-yellow, I agree that /etc/profile will be sourced at first, but it should be emphasized that ~/.bash_profile, ~/.bash_login, and ~/.profile will be looked for in sequence, and if one is found, the following files are ignored (the answer is partially right). I have verified it (I define JAVA_HOME in /etc/profile, and BASH_PROFILE_SOURED in ~/.bash_profile, BASH_LOGIN_SOURCED in ~/.bash_login, and BASH_DEFAULT_PROFILE_SOURCED in ~/.profile. In a newly login shell, only JAVA_HOME and BASH_PROFILE_SOURED is defined).Stiletto
It says "After that, three files are checked for existence" but then lists four. I'm pretty sure the problem here is that the first (/etc/profile) shouldn't be in the list.Mysterious
J
12

Since you probably know what a "shell" is and are using it your question only targets the difference between login shell and everything else...

A login shell only differs from any other shell by the fact that one or more initial setup scripts (resources) are loaded on startup, typically named with "profile" in their name. in there basic settings are defined that are derived to subsequently opened shells (so they only need to be defined once).

Jayson answered 12/8, 2013 at 12:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.