What are Interactive and Login Shells?#

Shell types are categorized by two orthogonal dimensions: Interactive and Login.

1. Technical Definitions#

1.1 Interactive Shell#

A shell is technically considered Interactive if:

  • It is started without non-option arguments (unless -s is specified) and without the -c option, and both its standard input and standard error are connected to terminals (as determined by isatty(3)). 1
  • It is explicitly started with the -i flag.

Verification:

  • [[ $- == *i* ]] && echo "Interactive" || echo "Non-interactive"
  • [[ -n "$PS1" ]] && echo "Interactive" || echo "Non-interactive"

1.2 Login Shell#

A shell is technically considered a Login Shell if and only if:

  • Its first argument (argv[0]) starts with a hyphen (eg. -bash). 2
  • It is explicitly started with the --login (or -l) flag.

Verification:

  • shopt login_shell (returns on for login shells)

This is why ssh host <command> is not a login shell: SSH invokes the shell as bash -c "<command>", which lacks both the leading hyphen and the login flag.

2. Shell Combinations#

There is no strict inclusion relationship between interactive and logins shells.

LoginNon-Login
InteractiveInteractive Login Shell
eg. SSH login, physical console
Interactive Non-Login Shell
eg. bash subshell
Non-InteractiveNon-Interactive Login Shell
eg. su - -c <cmd>3, bash --login -c <cmd>
Non-Interactive Non-Login Shell
eg. bash -c <cmd>, su -c <cmd>, ssh host <cmd>4, ./script.sh, Crontab

3. Configuration Files#

Startup file execution depends on the shell type. 5

  • Login Shell (Interactive or Non-Interactive):
    1. /etc/profile
    2. The first found file among ~/.bash_profile -> ~/.bash_login -> ~/.profile.

Login shells do not automatically execute ~/.bashrc. It is only executed because most default ~/.profile or ~/.bash_profile files contain a snippet to source it manually:

# if running bash
if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
	. "$HOME/.bashrc"
    fi
fi
  • Non-Login Shell
    • Interactive Non-Login Shell: ~/.bashrc
    • Non-Interactive Non-Login Shell: Usually none (reads $BASH_ENV if set).

3.1 $PATH and .profile#

The $PATH variable should ideally be set in ~/.profile rather than ~/.bashrc.

  • Inheritance: Environment variables set in a login shell (via ~/.profile) are inherited by all child processes. This means setting it once at login (~/.profile)is sufficient for all subshells.
  • Efficiency: Placing $PATH in ~/.bashrc causes it to be re-processed every time a subshell is opened, which is redundant and can lead to duplicated or excessively long entries.

References#


  1. bash(1): “An interactive shell is one started without non-option arguments (unless -s is specified) and without the -c option, and whose standard input and standard error are both connected to terminals (as determined by isatty(3)), or one started with the -i option.” ↩︎

  2. bash(1): “A login shell is one whose first character of argument zero is a -, or one started with the –login option.” ↩︎

  3. su(1): “Start the shell as a login shell with an environment similar to a real login.” The hyphen (-) is a shortcut for --login. The -c flag makes the shell non-interactive as it executes a command instead of reading from a terminal. ↩︎

  4. bash(1): “Bash attempts to determine when it is being run by the remote shell daemon, usually rshd, or the secure shell daemon sshd. If bash determines it is being run non-interactively in this fashion, it reads and executes commands from … ~/.bashrc.” When bash is run non-interactively by sshd, it is not a login shell, but it specifically reads ~/.bashrc anyway. ↩︎

  5. bash(1): “When bash is invoked as an interactive login shell… it 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, in that order, and reads and executes commands from the first one that exists and is readable.” ↩︎