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
相关推荐
Magnum Lehar39 分钟前
macos的信息采集器appledataHarvester-2
macos·网络安全·系统安全
echo-niuben2 小时前
macOS 端已如何正常安装并配置XcodeBuildMCP ?
macos
刘某某.2 小时前
Mac上缺失宋体字体,只有宋体-简
macos
Tom·Ge3 小时前
在macOS上安装OpenClaw并实现Chrome网站自动化测试
chrome·macos·策略模式
Lo-Y-eH4 小时前
Mac 安装 Ollama 部署 DeepSeek 模型
macos·ollama·deepseek·cherry studio
仙剑魔尊重楼21 小时前
iMazing 3.1.3官方中文版新功能介绍
macos·objective-c·cocoa
Genie cloud1 天前
在 Mac 上使用 Docker 安装宝塔并部署 LNMP 环境
macos·docker·容器·云计算
清蒸鳜鱼1 天前
【Mobile Agent——Droidrun】MacOS+Android配置、使用指南
android·macos·mobileagent
linweidong1 天前
屏幕尺寸的万花筒:如何在 iOS 碎片化生态中以不变应万变?
macos·ios·移动开发·objective-c·cocoa·ios面试·ios面经
TESmart碲视1 天前
如何用一套键盘鼠标切换多个输入设备?TESmart KVM热键工作原理深度解析.一份涵盖设计原理、使用方法与兼容性的完整技术指南
游戏·macos·计算机外设·kvm切换器·双屏kvm切换器