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
相关推荐
_可乐无糖1 小时前
内网穿透工具使用指南:ngrok 和 cpolar
安全·macos·云计算
不爱吃糖的程序媛2 小时前
鸿蒙PC命令行开发 macOS 上解决 pkg-config 命令未安装的问题
macos·华为·harmonyos
淘气淘的宝宝3 小时前
MAC intel 迁移至M4后brew及iterm2配置
macos
不爱吃糖的程序媛4 小时前
解决鸿蒙PC命令行编译 macOS 上 cp 命令参数冲突问题
macos·harmonyos·策略模式
Bruce_Liuxiaowei4 小时前
Mac_Linux 查询网站IP地址:4个核心命令详解
linux·tcp/ip·macos
逐梦苍穹4 小时前
ClamAV在macOS上的离线查杀与定时扫描实战(含clamd加速)
人工智能·安全·macos·策略模式·杀毒
Bigger17 小时前
Tauri(21)——窗口缩放后的”失焦惊魂”,游戏控制权丢失了
前端·macos·app
Bigger17 小时前
Tauri (20)——为什么 NSPanel 窗口不能用官方 API 全屏?
前端·macos·app
Wcowin1 天前
Mac Shell 环境优化指南
macos·职场和发展·蓝桥杯
止礼1 天前
FFmpeg8.0.1 Mac环境 CMake本地调试配置
macos·ffmpeg