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
-sis specified) and without the-coption, and both its standard input and standard error are connected to terminals (as determined byisatty(3)). 1 - It is explicitly started with the
-iflag.
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(returnsonfor 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.
| Login | Non-Login | |
|---|---|---|
| Interactive | Interactive Login Shell eg. SSH login, physical console | Interactive Non-Login Shell eg. bash subshell |
| Non-Interactive | Non-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):
/etc/profile- The first found file among
~/.bash_profile->~/.bash_login->~/.profile.
Login shells do not automatically execute
~/.bashrc. It is only executed because most default~/.profileor~/.bash_profilefiles 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_ENVif set).
- Interactive Non-Login Shell:
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
$PATHin~/.bashrccauses it to be re-processed every time a subshell is opened, which is redundant and can lead to duplicated or excessively long entries.
References#
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.” ↩︎
bash(1): “A login shell is one whose first character of argument zero is a -, or one started with the –login option.” ↩︎
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-cflag makes the shell non-interactive as it executes a command instead of reading from a terminal. ↩︎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
bashis run non-interactively bysshd, it is not a login shell, but it specifically reads~/.bashrcanyway. ↩︎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.” ↩︎