shell-startup-files
Work out which Bash/Zsh startup files run, in what order, for login vs non-login and interactive vs non-interactive shells. Use for any question about .bashrc, .bash_profile, .profile, .zshrc, .zshenv, .zprofile, /etc/profile, BASH_ENV, ZDOTDIR, path_helper, or a PATH, alias or env var that is missing, duplicated or reordered in ssh, cron, tmux, sudo/su, systemd, GUI apps or a new terminal, and when deciding where a shell setting belongs.
- License 许可
- MIT
- 最近更新
- 2026-11-11
- Stars
- ★ 0
- Forks
- 0
01 前言 Front Matter
- name
- shell-startup-files
- description
- Work out which Bash/Zsh startup files run, in what order, for login vs non-login and interactive vs non-interactive shells. Use for any question about .bashrc, .bash_profile, .profile, .zshrc, .zshenv, .zprofile, /etc/profile, BASH_ENV, ZDOTDIR, path_helper, or a PATH, alias or env var that is missing, duplicated or reordered in ssh, cron, tmux, sudo/su, systemd, GUI apps or a new terminal, and when deciding where a shell setting belongs.
02 资源文件 21
- 脚本assets/bash/bash_env.sh579 B
- 资产文件assets/bash/bash_logout259 B
- 资产文件assets/bash/bash_profile643 B
- 资产文件assets/bash/bashrc1.1 KB
- 资产文件assets/bash/profile819 B
- 资产文件assets/README.md1.5 KB
- 资产文件assets/system/environment.d-example.conf580 B
- 资产文件assets/system/paths.d-example17 B
- 脚本assets/system/profile.d-example.sh598 B
- 资产文件assets/zsh/zlogout181 B
- 资产文件assets/zsh/zprofile609 B
- 资产文件assets/zsh/zshenv766 B
- 资产文件assets/zsh/zshrc413 B
- 参考文档references/bash.md5.0 KB
- 参考文档references/platforms.md4.2 KB
- 参考文档references/sources.md5.2 KB
- 参考文档references/troubleshooting.md7.2 KB
- 参考文档references/zsh.md4.1 KB
- 脚本scripts/audit-startup-files.sh6.3 KB
- 脚本scripts/shell-context.sh1.7 KB
- 脚本scripts/trace-startup.sh5.1 KB
03 SKILL.md 正文
Bash and Zsh startup files
Which files a shell reads is decided by how the shell was started, not by the files themselves. Almost every "my PATH/alias/variable is missing" problem is a shell started in a different mode than the person assumed. Do not answer from file names. Classify the shell, read the order, check what the platform's own files chain to, then verify.
Workflow
- Classify the shell on two independent axes: login or not, interactive
or not. If the way it was started is not stated, ask, or have the person
source
scripts/shell-context.shin that shell. - Read the order from the tables below. For flags, sh mode, the sshd
case, ZDOTDIR or RCS options, read
references/bash.mdorreferences/zsh.md. - Check the wiring. The shell reads only the files in the tables.
/etc/profile.d,/etc/bashrcand ".bashrcat login" happen only because one file sources another, and that differs per platform:references/platforms.md. - Verify when the machine is reachable:
scripts/trace-startup.shfor the real order,scripts/audit-startup-files.shfor common mistakes. A trace outranks every table here. - Recommend a placement and say why that file is read in the contexts
that matter. Start new files from
assets/.
The two axes
| Axis | True when | Bash test | Zsh test |
|---|---|---|---|
| Login | argv[0] starts with - (login, sshd, su -), or -l |
shopt -q login_shell |
[[ -o login ]] |
| Interactive | no script or -c, and stdin+stderr are a terminal; or -i |
[[ $- == *i* ]] |
[[ -o interactive ]] |
| How it starts | Login | Interactive |
|---|---|---|
ssh host, console login, su -, sudo -i |
yes | yes |
| macOS terminal tab, tmux pane | yes | yes |
Linux terminal tab, typing bash/zsh, su user, sudo -s |
no | yes |
ssh host 'cmd', scripts, bash -c, cron |
no | no |
| systemd units, GUI apps | no shell at all |
Full list with screen, VS Code, git hooks and sudo details:
references/troubleshooting.md section 1.
Bash order
| Mode | Files, in order |
|---|---|
| Login | /etc/profile, then the first one only of ~/.bash_profile, ~/.bash_login, ~/.profile |
| Interactive non-login | (/etc/bash.bashrc on Debian, Ubuntu, Arch builds), then ~/.bashrc |
| Non-interactive | only the file named by $BASH_ENV |
| Login shell exit | ~/.bash_logout |
- A login shell does not read
~/.bashrc; the login file has to source it. - Creating
~/.bash_profilesilently disables~/.profile. ssh host 'cmd'reads~/.bashrcon most Linux builds although the shell is non-interactive. So~/.bashrcneeds an interactive guard near the top, and nothing above the guard may print (it breaks scp, sftp, rsync).
Zsh order
| # | File | Read when |
|---|---|---|
| 1 | /etc/zshenv, then $ZDOTDIR/.zshenv |
always |
| 2 | /etc/zprofile, then $ZDOTDIR/.zprofile |
login |
| 3 | /etc/zshrc, then $ZDOTDIR/.zshrc |
interactive |
| 4 | /etc/zlogin, then $ZDOTDIR/.zlogin |
login |
| exit | $ZDOTDIR/.zlogout, then /etc/zlogout |
login shell exit |
$ZDOTDIRmeans$HOMEwhen unset. Debian, Ubuntu and Arch keep the global files in/etc/zsh/..zshrcruns for every interactive shell, login or not..zshenvruns for every zsh including scripts, so keep it tiny and silent.- Zsh does not read
~/.profile,/etc/profileor any Bash file by itself. - macOS:
/etc/zprofilerunspath_helperafter~/.zshenv, which pushes PATH entries set there behind the system ones. Set PATH in~/.zprofile.
Where things belong
| Setting | Bash | Zsh |
|---|---|---|
| PATH and exported session environment | ~/.profile or ~/.bash_profile |
~/.zprofile |
Env that ssh host cmd and scripts need |
$BASH_ENV file, or above the guard in ~/.bashrc |
~/.zshenv |
| Aliases, functions, prompt, completion, options | ~/.bashrc |
~/.zshrc |
| All users | /etc/profile.d/NN-name.sh |
same, where the global zprofile sources /etc/profile |
| GUI apps, systemd, cron | not shell files: environment.d, unit Environment=, crontab variables |
same |
Exported variables are inherited, so once at login is enough. Aliases,
functions and options are not inherited, so every interactive shell must load
them. Full table: references/troubleshooting.md section 2.
Scripts
Run them; no need to read them first. Each has -h. Bash 3.2 compatible.
| Script | Use |
|---|---|
scripts/shell-context.sh |
Source it (. scripts/shell-context.sh) in the shell in question. Prints login/interactive state, argv0, SHLVL, ZDOTDIR, BASH_ENV. Works in Bash and Zsh. |
scripts/trace-startup.sh |
-s bash|zsh -m login-interactive|login|interactive|script. Prints the files actually read, in order. -S uses strace (Linux) and also shows files reached through source. -H DIR traces with another HOME. |
scripts/audit-startup-files.sh |
Read-only check of a home directory: shadowed .profile, missing rc delegation, missing guard, output before the guard, PATH in .zshenv on macOS. Exit 1 on warnings. Codes: references/troubleshooting.md section 4. |
If the scripts cannot be copied to the machine, the one-liners are:
PS4='+ ${BASH_SOURCE:-cmdline}:${LINENO}: ' bash -lixc : 2>&1 |
sed -nE 's/^\++ ([^:]+):[0-9]+: .*/\1/p' | awk '$0!=p{print; p=$0}' # not as root
zsh -o sourcetrace -lic exit
Templates
assets/README.md maps each template to its install path and explains the two
Bash layouts (Fedora/macOS style with .bash_profile, Debian style without).
assets/bash/:profile,bash_profile,bashrc,bash_logout,bash_env.shassets/zsh/:zshenv,zprofile,zshrc,zlogoutassets/system/:profile.d-example.sh,environment.d-example.conf,paths.d-example
Adapt a template to what the person already has; do not overwrite their files. Show the change as an edit to their file and tell them to back it up first.
References
| File | Read when |
|---|---|
references/bash.md |
Any Bash detail beyond the table: flags, sh/POSIX mode, the sshd case, BASH_ENV, tested matrix |
references/zsh.md |
ZDOTDIR, RCS/GLOBAL_RCS, -f, emulation, $path, what each zsh file is for |
references/platforms.md |
The answer depends on the OS or distro; anything about macOS path_helper; graphical logins |
references/troubleshooting.md |
A symptom to diagnose, the full context table, audit codes, common wrong beliefs |
references/sources.md |
The person cites an article, or two sources disagree: lists each source with its known errors |
Answering
State the shell kind first ("ssh host cmd gives a non-login, non-interactive
Bash"), then the ordered list of files actually read, then the fix and the
file it goes in, then a command to confirm. Correct wrong assumptions plainly;
the usual ones are in references/troubleshooting.md section 5. Use vim in
edit commands. Say when a claim is platform-dependent and not yet verified on
the person's machine.
04 同仓库更多 Skills
shell-script-expert
Use when the user asks "review this script", "write a bash script", "fix my shell script", "make this script safer", "why does this script fail", "convert this to a script", or working with .sh/.bash files, sh/bash syntax, shellcheck, cronjobs, and bash one-liners.
prose-synthesize
Turn unstructured notes into a structured, actionable plan. Use when given brain dumps, stream-of-consciousness, or scattered thoughts needing order.
prose-distill
Condense verbose text to its essence. Use when asked to condense, tighten, shorten, reduce verbosity, or omit needless words while preserving substance.
mindcraft
暂无简介
learn
Tutor for intellectual understanding: teach how or why something works rather than complete a task or provide an evaluative verdict. Trigger for explicit learning requests (teach, explain, ELI5, walkthroughs, quizzes, flashcards, definitions); terse concept prompts; confusion signals; prerequisite or sequencing questions; and conceptual questions about mechanisms, causes, or dynamics. Do not trigger for task completion (coding, writing, calculation, or translation), factual or news lookup, personal troubleshooting, resource recommendations, or opinion and interpretive verdicts.
deployment-readiness
Use to check deployment readiness of a host.