CODE/2023-06-06

Bash Startup Files: .bash_profile vs. .bashrc

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.
COMPANION ESSAYS / 思想脉络与互照篇目
同卷深读2026-05-22 ·

Installing Python 3 Alongside System Python on CentOS 7

同属「系统工程卷」脉络衍生思考与工程推演推演阅读 →
跨界互照2026-08-22 ·

我的书架:03|金融经管:经济运行、金融体系与企业治理

我的书架:03|金融经管:经济运行、金融体系与企业治理

跨领域对照思辨 · 从「思想随笔」视界互为印证推演阅读 →
RELATED INDEX / 相关阅读索引
柯航hang.ke·© 2014—2026
CC BY-NC 4.0·