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.
shopt -q login_shell && echo login || echo non-login [[ $- == *i* ]] && echo interactive || echo non-interactive
Try it
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.
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:
# ~/.bash_profile [ -f ~/.bashrc ] && . ~/.bashrc
Then:
- Put environment variables and
PATH(things that children inherit and need once per session) in~/.profileor~/.bash_profile. - Put aliases, functions, prompt,
shoptand readline settings (things only an interactive shell needs) in~/.bashrc. - Guard the top of
.bashrcso 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_profilesilently hides~/.profileunless it sources it. - Graphical sessions often do not read
.profileor.bashrcfor the desktop itself. Use~/.config/environment.d/*.confor your desktop's autostart for variables GUI apps need. - Cron has a minimal environment and runs non-login, non-interactive shells: no
.bashrc, a tinyPATH. Set what you need in the crontab or the script. - Scripts should not depend on startup files. Set
PATHand variables explicitly inside the script. $-holds the current option letters (containsiwhen interactive);$PS1is set only in interactive shells, which is another test.- Don't print from startup files in non-interactive cases:
scpandrsyncover ssh break if.bashrcwrites to stdout. - Reload after edits:
source ~/.bashrcor. ~/.bash_profileapplies changes without logging in again.