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
相关推荐
猫头虎3 小时前
如何解决 OpenClaw “Pairing required” 报错:两种官方解决方案详解
网络·windows·网络协议·macos·智能路由器·pip·scipy
皮卡车厘子1 天前
Mac 挂载目录
macos
良逍Ai出海1 天前
在 Windows & macOS 上安装 Claude Code,并使用第三方 Key 的完整教程
windows·macos
热爱生活的五柒1 天前
linux/mac/wsl如何使用claude code,并配置免费的硅基流动API?(官方的需要付费订阅)
linux·运维·macos
胖胖大王叫我来巡山1 天前
mac本地安装DataEase桌面版
macos
奋斗者1号1 天前
OpenClaw 部署方式对比:云端、WSL、Mac 本机、Ubuntu 虚拟机(2026年2月最新主流实践)
linux·ubuntu·macos
玉梅小洋1 天前
Android SDK 安装指南(MacOS 和 Windows)
android·windows·macos·sdk
2501_916007471 天前
没有 Mac 用户如何上架 App Store,IPA生成、证书与描述文件管理、跨平台上传
android·macos·ios·小程序·uni-app·iphone·webview
胖胖大王叫我来巡山1 天前
Mac通过源码安装部署SQLBOT
macos
June bug2 天前
【领域知识】广告全链路测试
macos·objective-c·cocoa