macOS中设置环境变量的各文件及作用域

在 macOS 中,~/.zshrc 和 ~/.bash_profile 是 Shell 的配置文件,用于设置环境变量、命令别名、启动命令等。它们在你每次打开终端时会被自动加载。

文件 对应 Shell 作用
~/.zshrc Zsh(macOS Catalina 及以后默认) 每次打开新的终端窗口/标签页时加载
~/.bash_profile Bash(macOS Mojave 及以前默认) 登录 Shell 时加载(如通过 SSH 登录)
~/.bashrc Bash 每次打开新的非登录 Shell 时加载(Linux 常见,macOS 默认不调用)

标题按 Shell 分类

Zsh 相关:

文件 作用
~/.zshrc 每次打开终端时加载(最常用)
~/.zprofile 登录 Shell 时加载(类似 .bash_profile
~/.zshenv 所有 Shell 都会加载(最早加载)
~/.zlogin 登录 Shell 时加载(在 .zprofile 之后)
~/.zlogout 退出登录 Shell 时加载

Bash 相关:

文件 作用
~/.bash_profile 登录 Shell 时加载
~/.bashrc 非登录 Shell 时加载
~/.bash_login 如果 .bash_profile 不存在,则尝试加载这个
~/.profile 通用文件,Bash 和其他 Shell 都可能加载(兼容性最好)
~/.bash_logout 退出登录 Shell 时加载

如何查看你当前用的是什么 Shell?

bash 复制代码
echo $SHELL
  • 输出 /bin/zsh → 你在用 Zsh
  • 输出 /bin/bash → 你在用 Bash

建议

  • 如果你用的是 Zsh,主要编辑:
bash 复制代码
nano ~/.zshrc
  • 如果你用的是 Bash,主要编辑:
bash 复制代码
nano ~/.bash_profile
相关推荐
黑化旺仔1 天前
【OC】KVO
macos·ios·objective-c·cocoa
Liekkas Kono1 天前
macOS 27 安装 VMware Fusion 和 Win11 经验
macos·win11·vmware
shark-chili3 天前
关于AI辅助编程的认知
数据库·人工智能·redis·macos·缓存
zwd20053 天前
Mac 上写英文遇到不会的词,有哪些工具?输入法、翻译软件、写作助手怎么选
人工智能·macos·效率工具·英语写作·输入法·deepl·翻译工具
33三 三like4 天前
Jx-Mac 本地部署过程总结
macos
大象AI共学4 天前
想在手机上读 Obsidian?Mac → iPhone / 安卓的零成本方案
macos·智能手机·iphone
sysinside5 天前
如何创建可引导的 macOS Golden Gate 27 安装介质
macos
Dr.Buho5 天前
macOS 27 升级教程:兼容机型、三种安装方法与降级提示
经验分享·macos
sysinside5 天前
macOS Golden Gate 27.0 (26A428) 正式版 Boot ISO 原版可引导映像下载
macos
黑科技iOS上架6 天前
Cocos应用App Store4.3被拒怎么自查
macos·ios·objective-c·cocoa·审核·ios混淆·cocos混淆