操作顺序:
-
bash打开 ~/.bash_profile文件:
sudo vi ~/.bash_profile
-
修改.bash_profile文件。添加环境变量:
export {变量}={变量值}
-
保存,重启bash生效
Mac 环境变量加载顺序
Mac系统的环境变量,加载顺序为:
- /etc/profile
- /etc/paths
- ~/.bash_profile
- ~/.bash_login
- ~/.profile
- ~/.bashrc
/etc/profile和/etc/paths是系统级别的,系统启动就会加载,后面几个是当前用户级的环境变量。后面3个按照从前往后的顺序读取,如果.bash_profile 文件存在,则后面的几个文件就会被忽略不读了,如果.bash_profile文件不存在,才会以此类推读取后面的文件。~/.bashrc没有上述规则,它是bash shell打开的时候载入的。