Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Concepts

Login vs interactive shells

Understand login, non-login, interactive and non-interactive Bash shells, how to detect each, and which startup files (.profile, .bashrc, /etc/profile) get read.

Bash behaves differently depending on how it was started. Two independent properties matter. A shell is a login shell if it is the first shell of a session (you logged in, or ran bash -l). It is interactive if it reads commands from a terminal you are typing at; scripts and bash -c are non-interactive. The combination decides which startup files run, so it decides where to put PATH changes, aliases and prompts.

Getting it wrong is a classic source of "it works in my terminal but not over ssh/cron/a script" confusion.

Terminal
shopt -q login_shell && echo login || echo non-login
[[ $- == *i* ]] && echo interactive || echo non-interactive

Try it

Bash Detect the kind of shell you are in

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash See which startup files load

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

What gets read

Shell kind Files read, in order
Interactive login /etc/profile, then the first one found of ~/.bash_profile, ~/.bash_login, ~/.profile
Interactive non-login ~/.bashrc (plus /etc/bash.bashrc on Debian family, patched in)
Non-interactive (scripts, bash -c) None, except the file named by $BASH_ENV if set
Non-interactive login (bash -lc) Same files as an interactive login
Started by sshd for a remote command ~/.bashrc (Bash detects it was started by sshd); not .profile
On logout (login shell) ~/.bash_logout
Invoked as sh /etc/profile and ~/.profile if login; $ENV if interactive

Terminals started by a desktop usually open non-login interactive shells (so .bashrc runs). A console login, ssh host with a terminal, su - and bash -l give login shells. macOS Terminal is unusual and opens login shells every time.

A reliable setup

Because login shells ignore .bashrc, nearly every distribution's default ~/.bash_profile or ~/.profile contains a bridge:

Terminal
# ~/.bash_profile
[ -f ~/.bashrc ] && . ~/.bashrc

Then:

  • Put environment variables and PATH (things that children inherit and need once per session) in ~/.profile or ~/.bash_profile.
  • Put aliases, functions, prompt, shopt and readline settings (things only an interactive shell needs) in ~/.bashrc.
  • Guard the top of .bashrc so non-interactive use stays quiet: case $- in *i*) ;; *) return ;; esac.

Usage notes

  • First file wins: a login shell reads only the first of .bash_profile, .bash_login, .profile. Creating ~/.bash_profile silently hides ~/.profile unless it sources it.
  • Graphical sessions often do not read .profile or .bashrc for the desktop itself. Use ~/.config/environment.d/*.conf or your desktop's autostart for variables GUI apps need.
  • Cron has a minimal environment and runs non-login, non-interactive shells: no .bashrc, a tiny PATH. Set what you need in the crontab or the script.
  • Scripts should not depend on startup files. Set PATH and variables explicitly inside the script.
  • $- holds the current option letters (contains i when interactive); $PS1 is set only in interactive shells, which is another test.
  • Don't print from startup files in non-interactive cases: scp and rsync over ssh break if .bashrc writes to stdout.
  • Reload after edits: source ~/.bashrc or . ~/.bash_profile applies changes without logging in again.