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
相关推荐
Magnetic_h3 小时前
【iOS】锁的原理
笔记·学习·macos·ios·objective-c·cocoa·xcode
Cosmoshhhyyy14 小时前
mac环境下安装git并配置密钥等
git·macos
肥肥呀呀呀20 小时前
mac 安卓模拟器 blueStacks
macos
csdn_aspnet20 小时前
如何在 MacOS 上安装 SQL Server
macos·sqlserver
共享家95272 天前
linux-数据链路层
linux·网络·macos
CZIDC2 天前
MacOS字体看起来比在 Windows 上更好?
macos
Cosmoshhhyyy3 天前
linux远程部署dify和mac本地部署dify
linux·运维·macos
行星0083 天前
mac 通过homebrew 安装和使用nvm
macos·npm·node.js
2501_928094653 天前
Mac电脑录屏工具 Omi录屏专家(Mac中文)
macos·mac·录屏工具·omi