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
相关推荐
大熊猫侯佩19 小时前
升级到 macOS26.5 后看视频会自动息屏的解决
macos·操作系统
磊 子21 小时前
C++ IO 流
macos·objective-c·cocoa
real_haha21 小时前
我做了一个仅有 1.3 MB 的 macOS 原生 AI 助手:AskNow
人工智能·macos
June bug1 天前
(Mac)macOS x86_64上onnxruntime==1.24.4 安装失败
macos
ACP广源盛139246256731 天前
iOS 27 开放 AI 生态@ACP#小型化扩展黄金风口,IX8008全面超越 ASM2806,铸就嵌入式 AI 扩展核心
人工智能·嵌入式硬件·macos·ios·计算机外设·objective-c·cocoa
海的辽阔2 天前
如何在MAC下安装EcomGpt模型
macos·大模型·ecomgpt
zandy10112 天前
hermes agent 安装教程 3.0:Win / Mac / Linux 全平台指南
linux·运维·macos
花开·莫之弃2 天前
Mac安装多版本jdk(jenv)
java·开发语言·macos
June bug2 天前
(Mac)torch==2.1.2 与 Python 3.12 不兼容+onnxruntime-silicon 不支持 Intel Mac
开发语言·python·macos
码农小北2 天前
MAC 配置鸿蒙(HarmonyOS) SDK 环境变量完整指南
macos·华为·harmonyos