Bash reads different startup files depending on whether a shell is a login shell, interactive, or non-interactive. Understanding the difference helps keep environment variables and aliases predictable.
Login shells
An interactive login Bash reads
/etc/profile, then the first readable file among ~/.bash_profile, ~/.bash_login, and ~/.profile. Only one of the user files is read. Distributions may add their own behavior through these files.Interactive non-login shells
Interactive non-login Bash reads
~/.bashrc. System-wide Bash configuration varies by distribution and may be sourced by a user startup file.Many configurations explicitly source
~/.bashrc from ~/.bash_profile so aliases and interactive settings are shared:bash_snippet
Bash · 3 lines|
| if [ -f ~/.bashrc ]; then |
| . ~/.bashrc |
| fi |
Place exported environment variables where the relevant login environment will read them, and interactive aliases and prompts in
~/.bashrc. Avoid printing output or assuming an interactive terminal in files used by non-interactive contexts.Applying changes
Start a new shell or source a file in the current shell:
bash_snippet
Bash · 1 line|
| source ~/.bashrc |
Sourcing runs the entire file again in the current shell, so repeated side effects such as appending to
PATH may accumulate. ~/.bash_logout is read when an interactive login shell exits. Non-interactive Bash follows different startup rules, including BASH_ENV when set.✦ 卷终 · 柯航存卷 ✦