Linux Command Reference ยท Bash Syntax
Bash startup files
Reference for which files Bash reads at startup: login versus non-login and interactive versus non-interactive shells, .bashrc, .bash_profile, .profile, BASH_ENV and logout.
Bash reads different startup files depending on how it was started. Two questions decide it: is the shell a login shell (the first shell of a session, such as one started by ssh, a text console, or bash -l), and is it interactive (reading commands from a terminal rather than running a script)?
Getting this wrong is the reason behind many "my alias works in the terminal but not over SSH" and "my PATH is wrong in cron" problems. The usual cure is a tiny ~/.bash_profile that sources ~/.bashrc, and keeping settings that only matter at the terminal in .bashrc.
~/.bash_profile # login shells (first of ~/.bash_profile, ~/.bash_login, ~/.profile) ~/.bashrc # interactive non-login shells ~/.bash_logout # when a login shell exits
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.
Syntax
| Situation | Files read |
|---|---|
| Login shell (interactive or not) | /etc/profile, then the first existing of ~/.bash_profile, ~/.bash_login, ~/.profile. |
| Interactive, not a login shell | /etc/bash.bashrc (Debian-style builds; compile-time option) and ~/.bashrc. |
Non-interactive shell (script, bash -c) |
None, except the file named in $BASH_ENV, if set. |
Started as sh |
Behaves like POSIX sh: login reads /etc/profile and ~/.profile; interactive reads $ENV. |
Non-interactive, but started by rshd or sshd (for example ssh host command) |
Reads ~/.bashrc. This is the one exception to "non-interactive shells read nothing". |
| Login shell exit | ~/.bash_logout. |
| Option | Effect |
|---|---|
-l, --login |
Act as a login shell. |
-i |
Act as an interactive shell. |
--norc |
Do not read ~/.bashrc in an interactive shell. |
--noprofile |
Do not read the login files. |
--rcfile FILE |
Read FILE instead of ~/.bashrc. |
--posix |
Follow POSIX behavior; reads $ENV when interactive. |
Returns: startup files are sourced, so errors in them do not stop the shell; a syntax error is printed and the rest of the file is skipped.
Errors and edge cases: $BASH_ENV is expanded, but its value is not searched in PATH. Never set it in a setuid context.
Usage notes
- Only one of three login files is read:
.bash_profilewins over.bash_login, which wins over.profile. If you create~/.bash_profileand forget to source~/.profile, settings from.profilesilently stop applying. - Terminals on Linux desktops usually start non-login interactive shells, so
.bashrcruns but.bash_profiledoes not. On macOS, Terminal.app starts login shells by default, so the reverse is true. - What goes where. Environment variables (
PATH,EDITOR) belong in the login file so every child process inherits them. Aliases, functions, the prompt andshoptsettings belong in.bashrc. - Interactive guard. Start
.bashrcwithcase $- in *i*) ;; *) return;; esacso that it does nothing and prints nothing when something sources it from a script. - Never print in startup files of non-interactive shells. Output breaks
scp,rsyncandsftp, which expect a silent remote shell. - Cron and systemd do not start login shells, so they do not read your profile. Set
PATHexplicitly in the crontab or unit file. - Debugging.
bash -x -lorPS4='+ ${BASH_SOURCE}:${LINENO}: ' bash -xlshows every line executed from the startup files. - Non-login subshell. Running
bashinside a terminal reads.bashrcagain, but not the profile, so exported variables are inherited and not re-run.