This article describes how to change the Path environment variable in Mac environment, such as configuring Java, mysql, logcat, etc. All are very simple content, but after the basic configuration once, do not need to worry about, so it is inevitable that I cannot remember, so WRITE an article to help myself mark.

In Mac, bash is used as the default shell. The environment variables of the Mac system are loaded in the order (top-down) :

  • /etc/profile
  • /etc/paths
  • ~/.bash_profile
  • ~/.bash_login
  • ~/.profile
  • ~/.bashrc

/etc/profile and /etc/paths are system level variables that are loaded upon system startup, and the rest are environment variables at the current user level.

If the ~/.bash_profile file exists, the following files are ignored;

If the ~/.bash_profile file does not exist, subsequent files are read.

~/.bashrc has no such rule and is loaded when bash shell is opened.

If not specified, the syntax for PATH is:

The export PATH = $PATH: < 1 > PATH: < 2 > PATH: < PATH > 3: -- -- -- -- -- - : < N > PATH (PATH using separated by a colon) amongCopy the code

(1) Global Settings

The following file Settings are global and require root permission to modify

sudo chmod -R 777 paths
Copy the code

1) /etc/paths

Edit Paths by adding environment variables to the PATHS file, one path at a time,

What about chestnuts? The paths file reads as follows:

/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin
/usr/local/mysql/bin
Copy the code

2) /etc/profile (it is recommended not to modify this file)

Global (public) configuration, which is read by any user when they log in.

3) /etc/bashrc (where system-level environment variables are added)

Global (public) configuration, which the Bash shell reads either way when it executes.

4) Add environment variables

1. Create a file in /etc/paths.d: sudo touch /etc/paths.d/mysql

The list of files under paths.d is as follows:

-rw-r--r-- 1 root wheel 24B 10 29 16:42 mysql -rw-r--r-- 1 root wheel 24B 10 29 15:43 dotnet -rw-r--r-- 1 root wheel 15B  10 29 15:43 dotnet-cli-tools -rw-r--r-- 1 root wheel 61B 10 25 11:25 mono-commands -rw-r--r-- 1 root wheel 68B 10 25 12:33 workbooksCopy the code

Note: Mysql is created manually, and others are created when dotnet is installed.

2. Open this file with vim (edit is not allowed if open-t is used) :

sudo vim /etc/paths.d/mysql

3. Edit the file, type the path and save it (close the Terminal window and open a new one to use mysql)

/usr/local/mysql/bin

This makes it easier to manage variables without putting them all in a Paths file.

(2) Current user Settings

1) ~/.bash_profile (add user-level environment variables to any file)

Note: Linux is.bashrc and Mac is.bash_profile

This file is read only when the bash shell is executed as login. This file is executed only once!

Setting environment variables:

export PATH=/opt/local/bin:/opt/local/sbin:$PATH
Copy the code

By default, you can set some other environment variables in it, such as alias ll= ‘ls-la’

2) ~/

To take effect immediately, execute the following statement: $source corresponding file, i.e.

source ~/.bash_profile
Copy the code

Generally, after environment variables are changed, they take effect after a restart.