macOS环境变量
macOS下的 bash 环境变量存放路径及覆盖生效层级。
export
:显示所有环境变量
set
:显示全部环境变量
echo
:显示单个环境变量,例如 echo $PATH,echo $HOME,echo $SHELL
由于export变量的父子传递特性,如果想一直保持一个UNIX环境变量,必须到 /etc/profile
或 /etc/bashrc
或 ~/.bash_profile
或 ~/.bashrc
或 ~/.profile
中配置,在其他地方定义和 export 都不会生效。
Mac系统的环境变量加载顺序为:
/etc/profile
/etc/paths
~/.bash_profile
~/.bash_login
~/.profile
~/.bashrc
说明:
/etc/profile
和/etc/paths
是系统级别的,系统启动就会加载;后面4个是当前用户级的环境变量。- 3-5按照从前往后的顺序读取:
- 如果
~/.bash_profile
文件存在,则后面2个文件就被忽略不读了; - 如果
~/.bash_profile
文件不存在,才会依此类推读取后面的文件。
- 如果
~/.bashrc
没有上述规则,它是 bash shell 打开的时候载入的。
关于配置文件的加载顺序:请重点阅读 man bash
手册中的 INVOCATION、RESTRICTED SHELL 和 FILES 等章节。
/etc/profile#
全局(公有)配置,不管是哪个用户,登录时都会读取该文件。
$ cat /etc/profile
# System-wide .profile for sh(1)
if [ -x /usr/libexec/path_helper ]; then
eval `/usr/libexec/path_helper -s`
fi
if [ "${BASH-no}" != "no" ]; then
[ -r /etc/bashrc ] && . /etc/bashrc
fi
可执行 man path_helper
查看相关说明。
建议不修改这个文件
/etc/bashrc#
全局(公有)配置,bash shell执行时,不管是何种方式,都会读取此文件。
$ cat /etc/bashrc
# System-wide .bashrc file for interactive bash(1) shells.
if [ -z "$PS1" ]; then
return
fi
PS1='\h:\W \u\$ '
# Make bash check its window size after a process completes
shopt -s checkwinsize
[ -r "/etc/bashrc_$TERM_PROGRAM" ] && . "/etc/bashrc_$TERM_PROGRAM"
注意:
一般在这个文件中添加系统级环境变量。
请不要在~/.bashrc
中设置 PATH ,否则会导致 PATH 中目录的意外增长。因为每次打开一个新的 shell终端窗口,都会读取~/.bashrc
。
/etc/paths#
编辑 paths,将环境变量添加到 paths文件中 ,一行一个路径。
全局建议修改这个文件
/etc/paths.d#
以下摘录自 man path_helper
:
DESCRIPTION
The path_helper utility reads the contents of the files in the directories /etc/paths.d and /etc/manpaths.d
and appends their contents to the PATH and MANPATH environment variables respectively. (The MANPATH envi-
ronment variable will not be modified unless it is already set in the environment.)
Files in these directories should contain one path element per line.
Prior to reading these directories, default PATH and MANPATH values are obtained from the files /etc/paths
and /etc/manpaths respectively.
How to use /etc/paths.d to add executable files to my path?
The paths in /etc/paths.d/
are added to the path by /usr/libexec/path_helper
, which is run from /etc/profile
, /etc/zprofile
, and /etc/csh.login
.
Use /etc/paths or /etc/paths.d to add items to the PATH in macOS Sierra?
Use /etc/paths.d
.
The primary reason is that /etc/paths
will be modified and/or replaced by system upgrades. /etc/paths.d/
items will not.
Files are generally named with the pattern index-source. E.g., 99-mypaths
. Paths are appended in order*.
It's a lot easier to simply add/remove a file than programmatically editing one idempotently without bugs.
The default /etc/profile
, /etc/zprofile
and /etc/csh.login
on macOS all load path_helper.
How can one use /etc/paths.d to add a path with spaces in it to $PATH?
demo#
以下为 macOS ZSH 下的 PATH 变量:
# print为默认动作,可省略
$ echo $PATH | awk 'BEGIN{RS=":"} {print}'
# 省略在 ~/.zshrc 中追加在 PATH 前面的配置
/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin
/opt/X11/bin
/Library/Apple/usr/bin
/Applications/Wireshark.app/Contents/MacOS
# 省略在 ~/.zshrc 中追加在 PATH 后面的配置
前面5个为 /etc/paths
中预定义的路径;后面3个为 /etc/paths.d
中配置的环境变量:
每个文件里面包含一行可执行路径,被 /etc/profile
或 /etc/zprofile
执行 path_helper 加载到 PATH 环境变量中:
$ cat /etc/paths.d/40-XQuartz
/opt/X11/bin
$ cat /etc/paths.d/100-rvictl
/Library/Apple/usr/bin
$ cat /etc/paths.d/Wireshark
/Applications/Wireshark.app/Contents/MacOS
~/.bash_profile#
当 bash shell 是以 login 方式执行时,才会读取此文件,该文件仅仅执行一次。
一般在这个文件中添加用户级环境变量
refs#
Linux#
Linux下环境变量设置
Linux设置环境变量的三种方法
Linux操作系统下三种配置环境变量的方法
设置Linux环境变量的方法和区别_Ubuntu
macOS#
Where is the default terminal $PATH located on Mac?
How To Edit Your PATH Environment Variables On Mac OS X
MY MAC OSX BASH PROFILE
MAC OS X上找不到.bash_profile?
Creating a .bash_profile on your mac
macOS 设置环境变量的方法
macOS 设置环境变量 PATH
macOS 添加环境变量的三种方法
How To Add /usr/local/bin In $PATH On Mac
Setting PATH environment variable in OSX permanently
How do I set the global PATH environment variable on OS X?