Mac generally uses bash as the default shell, and the system environment variable loading order is:
/etc/profile /etc/paths ~/.bash_profile ~/.bash_login ~/.profile ~/.bashrc
Of course, /etc/profile and /etc/paths are system-level, and they will be loaded when the system starts. The latter are the current user-level environment variables. The last three are read in order from front to back. If the ~/.bash_profile file exists, the following files will be ignored and not read. If the ~/.bash_profile file does not exist, it will be read by analogy. later files. ~/.bashrc does not have the above rules, it is loaded when the bash shell is opened.
Unless otherwise specified, the syntax for setting PATH is:
#中间用冒号隔开
export PATH=$PATH:<PATH 1>:<PATH 2>:<PATH 3>:------:<PATH N>
(1) Global settings
The following file settings are global and require root privileges to modify
1) /etc/paths (it is recommended to modify this file globally)
Edit paths, add environment variables to the paths file, one line per line
Hint: When entering environment variables, you don't need to enter them one by one, just drag the folder to Terminal.
2) /etc/profile (it is recommended not to modify this file)
Global (public) configuration, which is read when logging in, regardless of the user.
3) /etc/bashrc (usually add system-level environment variables to this file)
Global (public) configuration, this file is read when the bash shell executes, regardless of the method.
4) Environment variable modification operation
- Create a file:
sudo touch /etc/paths.d/mysql - Open this file with vim (if it is opened with open -t, editing is not allowed):
sudo vim /etc/paths.d/mysql - Edit the file, type the path and save it (close the Terminal window and reopen one, you can use the mysql command)
/usr/local/mysql/bin
It is said that you can generate a new file yourself, without putting all variables in paths One file for easy management.
(2) Single user settings
1) ~/.bash_profile (add user-level environment variables to any file)
(Note: Linux is .bashrc and Mac is .bash_profile)
If the bash shell is executed in login mode, this file will be read. This file is executed only once! By default, it sets some environment variables
. Set the command alias alias ll='ls -la' and set the environment variables:
export PATH=/opt/local/bin:/opt/local/sbin:$PATH
2) ~/.bashrc same as above
If you want to take effect immediately, you can execute the following statement:
$ source corresponding file After the
general environment variable is changed, it will take effect after restarting.