Mac 环境变量配置基础教程

MacOS 下一般配置有多个 Shell,如 Bash、ZSH 等,不同的 Shell 其创建 Terminal 时使用的环境变量配置文件也不尽相同,但一般都会读取并执行脚本文件 /etc/profile 来加载系统级环境变量,而用户级别环境变量,一般都会在各自 Shell 的 Home 路径下配置,即 Bash~/.bash_profileZSh~/.zprofile,以及 SH 的 ~/.profile 等。

配置系统环境变量

为了尽量统一控制系统级别环境变量,同时支持可插拔,因此本文参考了 Ubuntu 的 Bash Shell 加载方式,通过修改 /etc/profile 脚本来实现。

/etc/profile 脚本中增加下列内容(如果没有写入权限,建议先用 sudo 来进行 chmod +w,之后再执行 chmod -w 还原权限):

bash 复制代码
if [ -d /etc/profile.d ]; then
  for i in /etc/profile.d/*.sh; do
    if [ -r $i ]; then
      . $i
    fi
  done
  unset i
fi

然后创建 /etc/profile.d 文件夹,并在其中增加需要加载环境变量的 sh 脚本,如:

bash 复制代码
# /etc/profile.d/
$ tree /etc/profile.d/
/etc/profile.d/
└── env.sh

# env.sh
eval "$(/opt/homebrew/bin/brew shellenv)"

修改用户环境变量

用户级别环境变量,一般都会在各自 Shell 的 Home 路径下配置,即 Bash~/.bash_profileZSh~/.zprofile,以及 SH 的 ~/.profile 等。

因为不同 Shell 的用户路径和环境变量配置脚本不同,因此可能无法实现统一配置,目前还是需要在各自的脚本文件中分别多次配置。

当然,也可以将相同的配置抽离成一个公共的脚本文件,每次在配置时,都执行其脚本即可。

验证结果

关闭所有 Terminal 后重启 Shell,然后验证环境变量是否配置成功。

bash 复制代码
brew help
相关推荐
白玉cfc32 分钟前
【iOS】KVC 与 KVO 的基本了解与使用
macos·ios·objective-c·cocoa
马拉萨的春天42 分钟前
谈谈你对iOS的runtime和runloop的了解
macos·ios·cocoa
xingxing_F1 小时前
A Better Finder Rename for mac 文件批量重命名
macos
0wioiw01 小时前
RealVNC Viewer(Windows控制Mac)
macos
xingxing_F15 小时前
Beyond Compare 5 for Mac 好用的文件对比工具
macos
一张假钞16 小时前
Mac OS 安装 VirtualBox
macos·virtualbox
哦豁灬16 小时前
macOS 基本使用
macos
aerror18 小时前
如何解压一个MacOs PKG 安装包修改内容后重新打包
macos
猫头虎18 小时前
openAI发布的AI浏览器:什么是Atlas?(含 ChatGPT 浏览功能)macOS 离线下载安装Atlas完整教程
人工智能·macos·chatgpt·langchain·prompt·aigc·agi
fukai772218 小时前
uTools 最新版优化macOS 26 高 GPU 占用问题
macos