zsh-path-skill
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseZsh PATH Management Skill
Zsh PATH 管理指南
This skill provides comprehensive guidance for managing PATH environment variables in zsh, including troubleshooting missing commands, adding new tools, and organizing shell configuration.
本文档提供了在zsh中管理PATH环境变量的全面指导,包括排查命令缺失问题、添加新工具以及整理shell配置等内容。
When to Use This Skill
适用场景
Use this skill when:
- Getting "command not found" errors for installed tools
- Adding new tools to PATH (bun, nvm, cargo, go, Python venv, etc.)
- Validating and auditing current PATH entries
- Organizing PATH configuration between .zshrc and .zshrc.local
- Troubleshooting shell startup issues related to PATH
- Setting up version managers (nvm, pyenv, rbenv)
在以下场景中使用本指南:
- 已安装的工具出现“command not found”错误
- 将新工具(如bun、nvm、cargo、go、Python venv等)添加到PATH
- 验证和审计当前PATH条目
- 在.zshrc和.zshrc.local之间整理PATH配置
- 排查与PATH相关的shell启动问题
- 配置版本管理器(nvm、pyenv、rbenv)
Diagnostic Commands
诊断命令
Check Current PATH
查看当前PATH
bash
undefinedbash
undefinedList all PATH entries
List all PATH entries
echo $PATH | tr ':' '\n'
echo $PATH | tr ':' '\n'
Check if specific command is in PATH
Check if specific command is in PATH
which bun 2>/dev/null || echo "bun not found in PATH"
which bun 2>/dev/null || echo "bun not found in PATH"
Find where a command is installed
Find where a command is installed
command -v node
type -a python
undefinedcommand -v node
type -a python
undefinedValidate PATH Entries
验证PATH条目
bash
undefinedbash
undefinedCheck for broken/non-existent PATH entries
Check for broken/non-existent PATH entries
echo $PATH | tr ':' '\n' | while read p; do
[[ -d "$p" ]] || echo "MISSING: $p"
done
echo $PATH | tr ':' '\n' | while read p; do
[[ -d "$p" ]] || echo "MISSING: $p"
done
Check for duplicate PATH entries
Check for duplicate PATH entries
echo $PATH | tr ':' '\n' | sort | uniq -d
undefinedecho $PATH | tr ':' '\n' | sort | uniq -d
undefinedCommon Tool PATH Configurations
常见工具的PATH配置
Bun (JavaScript runtime)
Bun(JavaScript 运行时)
bash
undefinedbash
undefinedAdd to .zshrc
Add to .zshrc
export BUN_INSTALL="$HOME/.bun"
export PATH="$BUN_INSTALL/bin:$PATH"
Default location: `~/.bun/bin/bun`export BUN_INSTALL="$HOME/.bun"
export PATH="$BUN_INSTALL/bin:$PATH"
默认位置:`~/.bun/bin/bun`NVM (Node Version Manager)
NVM(Node 版本管理器)
bash
undefinedbash
undefinedAdd to .zshrc
Add to .zshrc
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && . "$NVM_DIR/bash_completion"
Default location: `~/.nvm/`
**Note:** NVM is a shell function, not a binary. It modifies PATH dynamically to point to the active Node version.export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && . "$NVM_DIR/bash_completion"
默认位置:`~/.nvm/`
**注意:** NVM是一个shell函数,而非二进制文件。它会动态修改PATH以指向当前激活的Node版本。Python Virtual Environment
Python 虚拟环境
bash
undefinedbash
undefinedAdd to .zshrc for global tools venv
Add to .zshrc for global tools venv
export PATH="$HOME/.venv/tools3/bin:$PATH"
export PATH="$HOME/.venv/tools3/bin:$PATH"
For pyenv
For pyenv
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
undefinedexport PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
undefinedRust/Cargo
Rust/Cargo
bash
undefinedbash
undefinedAdd to .zshrc
Add to .zshrc
export PATH="$HOME/.cargo/bin:$PATH"
undefinedexport PATH="$HOME/.cargo/bin:$PATH"
undefinedGo
Go
bash
undefinedbash
undefinedAdd to .zshrc
Add to .zshrc
export GOPATH="$HOME/go"
export PATH="$GOPATH/bin:$PATH"
undefinedexport GOPATH="$HOME/go"
export PATH="$GOPATH/bin:$PATH"
undefinedHomebrew (macOS)
Homebrew(macOS)
bash
undefinedbash
undefinedIntel Mac
Intel Mac
export PATH="/usr/local/bin:$PATH"
export PATH="/usr/local/bin:$PATH"
Apple Silicon Mac
Apple Silicon Mac
export PATH="/opt/homebrew/bin:$PATH"
eval "$(/opt/homebrew/bin/brew shellenv)"
undefinedexport PATH="/opt/homebrew/bin:$PATH"
eval "$(/opt/homebrew/bin/brew shellenv)"
undefinedLocal bin directories
本地bin目录
bash
undefinedbash
undefinedUser-local binaries
User-local binaries
export PATH="$HOME/.local/bin:$PATH"
export PATH="$HOME/bin:$PATH"
undefinedexport PATH="$HOME/.local/bin:$PATH"
export PATH="$HOME/bin:$PATH"
undefinedFile Organization Best Practices
文件组织最佳实践
.zshrc (Team/Shared Configuration)
.zshrc(团队/共享配置)
Keep in .zshrc:
- Oh My Zsh configuration
- Common team aliases
- Standard PATH additions (homebrew, local bin)
- Plugin configuration
bash
undefined建议在.zshrc中保存:
- Oh My Zsh配置
- 团队通用别名
- 标准PATH添加项(如homebrew、本地bin)
- 插件配置
bash
undefined===== ZSH-TOOL MANAGED SECTION BEGIN =====
===== ZSH-TOOL MANAGED SECTION BEGIN =====
Team-wide PATH modifications
Team-wide PATH modifications
export PATH="$HOME/.local/bin:$PATH"
export PATH="$HOME/bin:$PATH"
export PATH="$HOME/.local/bin:$PATH"
export PATH="$HOME/bin:$PATH"
Bun
Bun
export BUN_INSTALL="$HOME/.bun"
export PATH="$BUN_INSTALL/bin:$PATH"
export BUN_INSTALL="$HOME/.bun"
export PATH="$BUN_INSTALL/bin:$PATH"
NVM
NVM
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
===== ZSH-TOOL MANAGED SECTION END =====
===== ZSH-TOOL MANAGED SECTION END =====
Load local customizations
Load local customizations
[[ -f ~/.zshrc.local ]] && source ~/.zshrc.local
undefined[[ -f ~/.zshrc.local ]] && source ~/.zshrc.local
undefined.zshrc.local (Personal/Machine-Specific)
.zshrc.local(个人/机器专属配置)
Keep in .zshrc.local:
- Machine-specific PATH entries
- Personal tool configurations
- Experimental settings
- Integrations that may override keybindings
bash
undefined建议在.zshrc.local中保存:
- 机器专属的PATH条目
- 个人工具配置
- 实验性设置
- 可能覆盖按键绑定的集成项
bash
undefinedMachine-specific tools
Machine-specific tools
export PATH="/opt/custom-tool/bin:$PATH"
export PATH="/opt/custom-tool/bin:$PATH"
Personal integrations
Personal integrations
if command -v atuin >/dev/null 2>&1; then
eval "$(atuin init zsh)"
fi
undefinedif command -v atuin >/dev/null 2>&1; then
eval "$(atuin init zsh)"
fi
undefinedPATH Order Matters
PATH的顺序至关重要
PATH is searched left-to-right. First match wins.
bash
undefinedPATH的搜索顺序是从左到右,第一个匹配的命令会被优先使用。
bash
undefinedThis order means ~/.local/bin takes priority over system bins
This order means ~/.local/bin takes priority over system bins
export PATH="$HOME/.local/bin:/usr/local/bin:/usr/bin:/bin"
undefinedexport PATH="$HOME/.local/bin:/usr/local/bin:/usr/bin:/bin"
undefinedRecommended PATH Order
推荐的PATH顺序
- Project-specific bins (added by direnv)
- User local bins (,
~/.local/bin)~/bin - Language version managers (nvm, pyenv, rbenv)
- Package managers (homebrew, cargo)
- System paths (,
/usr/local/bin)/usr/bin
- 项目专属bin目录(由direnv添加)
- 用户本地bin目录(、
~/.local/bin)~/bin - 语言版本管理器(nvm、pyenv、rbenv)
- 包管理器(homebrew、cargo)
- 系统路径(、
/usr/local/bin)/usr/bin
Troubleshooting
问题排查
Command Not Found After Installation
安装后出现Command Not Found错误
bash
undefinedbash
undefined1. Find where tool was installed
1. Find where tool was installed
ls -la ~/.bun/bin/bun 2>/dev/null
ls -la ~/.cargo/bin/rustc 2>/dev/null
ls -la ~/.nvm/versions/node/*/bin/node 2>/dev/null
ls -la ~/.bun/bin/bun 2>/dev/null
ls -la ~/.cargo/bin/rustc 2>/dev/null
ls -la ~/.nvm/versions/node/*/bin/node 2>/dev/null
2. Check if PATH includes the directory
2. Check if PATH includes the directory
echo $PATH | tr ':' '\n' | grep -E "(bun|cargo|nvm)"
echo $PATH | tr ':' '\n' | grep -E "(bun|cargo|nvm)"
3. Add missing PATH entry to .zshrc
3. Add missing PATH entry to .zshrc
4. Reload shell
4. Reload shell
source ~/.zshrc
source ~/.zshrc
or
or
exec $SHELL
undefinedexec $SHELL
undefinedPATH Changes Not Taking Effect
PATH修改未生效
bash
undefinedbash
undefinedCheck which file sets the variable
Check which file sets the variable
grep -r "export PATH" ~/.zshrc ~/.zshrc.local ~/.zprofile 2>/dev/null
grep -r "export PATH" ~/.zshrc ~/.zshrc.local ~/.zprofile 2>/dev/null
Source the correct file
Source the correct file
source ~/.zshrc
source ~/.zshrc
Or start fresh shell
Or start fresh shell
exec $SHELL
undefinedexec $SHELL
undefinedStartup Hook Errors
启动钩子错误
Common causes:
- Missing PATH entries for tools used in hooks
- Broken references to non-existent directories
- Version managers not finding expected versions
bash
undefined常见原因:
- 启动钩子中使用的工具对应的PATH条目缺失
- 引用了不存在的目录
- 版本管理器未找到预期的版本
bash
undefinedDebug startup
Debug startup
zsh -x 2>&1 | head -100
zsh -x 2>&1 | head -100
Check for errors in specific config
Check for errors in specific config
bash -n ~/.zshrc
undefinedbash -n ~/.zshrc
undefinedDuplicate PATH Entries
重复的PATH条目
bash
undefinedbash
undefinedRemove duplicates (add to .zshrc)
Remove duplicates (add to .zshrc)
typeset -U PATH path
undefinedtypeset -U PATH path
undefinedVerification Commands
验证命令
After making PATH changes:
bash
undefined修改PATH后,执行以下命令验证:
bash
undefinedReload configuration
Reload configuration
source ~/.zshrc
source ~/.zshrc
Verify tool is accessible
Verify tool is accessible
which bun && bun --version
which node && node --version
command -v nvm && nvm --version
which bun && bun --version
which node && node --version
command -v nvm && nvm --version
Verify PATH includes new entries
Verify PATH includes new entries
echo $PATH | tr ':' '\n' | grep -E "(bun|nvm|venv)"
undefinedecho $PATH | tr ':' '\n' | grep -E "(bun|nvm|venv)"
undefinedQuick Reference
快速参考
| Tool | PATH Entry | Check Command |
|---|---|---|
| bun | | |
| nvm | | |
| cargo | | |
| go | | |
| pyenv | | |
| brew (ARM) | | |
| brew (Intel) | | |
| 工具 | PATH条目 | 验证命令 |
|---|---|---|
| bun | | |
| nvm | | |
| cargo | | |
| go | | |
| pyenv | | |
| brew (ARM) | | |
| brew (Intel) | | |
Common Patterns
常见配置模式
Conditional PATH Addition
条件式PATH添加
bash
undefinedbash
undefinedOnly add if directory exists
Only add if directory exists
[[ -d "$HOME/.bun/bin" ]] && export PATH="$HOME/.bun/bin:$PATH"
[[ -d "$HOME/.bun/bin" ]] && export PATH="$HOME/.bun/bin:$PATH"
Only add if command not already available
Only add if command not already available
command -v bun >/dev/null || export PATH="$HOME/.bun/bin:$PATH"
undefinedcommand -v bun >/dev/null || export PATH="$HOME/.bun/bin:$PATH"
undefinedLazy Loading for Faster Startup
延迟加载以加快启动速度
bash
undefinedbash
undefinedLazy load nvm (faster shell startup)
Lazy load nvm (faster shell startup)
lazy_load_nvm() {
unset -f nvm node npm npx
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
}
nvm() { lazy_load_nvm && nvm "$@"; }
node() { lazy_load_nvm && node "$@"; }
npm() { lazy_load_nvm && npm "$@"; }
npx() { lazy_load_nvm && npx "$@"; }
undefinedlazy_load_nvm() {
unset -f nvm node npm npx
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
}
nvm() { lazy_load_nvm && nvm "$@"; }
node() { lazy_load_nvm && node "$@"; }
npm() { lazy_load_nvm && npm "$@"; }
npx() { lazy_load_nvm && npx "$@"; }
undefined