As you know Mac is a wonderful OS, if you are using MacOS, you may need to know how to edit your PATH environment. The good news is that this is an easy task on Mac OS
1. What are Environment Variables?
Environment variables are global system variables accessible by all the processes/users running under the Operating System (OS), such as Windows, macOS and Linux. Environment variables are useful to store system-wide values, for examples,
- PATH: the most frequently-used environment variable, which stores a list of directories to search for executable programs.
- OS: the operating system.
- COMPUTENAME, USERNAME: stores the computer and current user name.
- SystemRoot: the system root directory.
- (Windows) HOMEDRIVE, HOMEPATH: Current user’s home directory.
2. (macOS/Linux) Environment Variables
Environment variables in macOS/Unixes are case-sensitive. Global environment variables (available to ALL processes) are named in uppercase, with words joined with underscore (_), e.g., JAVA_HOME. Local variables (available to the current process only) are in lowercase. The recommended way is by editing your .bash_profile file. This file is read and the commands in it executed by Bash every time you log in to the system. The best part is that this file is specific to your user so you won?t affect other users on the same system by changing it.
2.1 ? Using Environment Variables in Bash Shell
Most of the Unixes (Ubuntu/macOS) use the so-called Bash shell. Under bash shell:
- To list all the environment variables, use the command ?env” (or “printenv”). You could also use “set” to list all the variables, including all local variables.
- To reference a variable, use $varname, with a prefix ‘$’ (Windows uses %varname%).
- To print the value of a particular variable, use the command ?echo $varname”.
- To set an environment variable, use the command ?export varname=value”, which sets the variable and exports it to the global environment (available to other processes). Enclosed the value with double quotes if it contains spaces.
- To set a local variable, use the command ?varname=value” (or “set varname=value”). Local variable is available within this process only.
- To unset a local variable, use command ?varname=”, i.e., set to empty string (or “unset varname”).
2.2 ? How to Set an Environment Variable Permanently in Bash Shell
You can set an environment variable permanently by placing an export command in your Bash shell’s startup script “~/.bashrc” (or “~/.bash_profile”, or “~/.profile”) of your home directory; or “/etc/profile” for system-wide operations. Take note that files beginning with dot (.) is hidden by default. To display hidden files, use command “ls -a” or “ls -al”.
For example, to add a directory to the PATH environment variable, add the following line at the end of “~/.bashrc” (or “~/.bash_profile”, or “~/.profile”), where ~ denotes the home directory of the current user, or “/etc/profile” for ALL users.
Step 1: Open up a Terminal window
Step 2: Enter the follow commands:
touch ~/.bash_profile; open ~/.bash_profile
You can use nano-editor to make it great, the file allows you to customize the environment your user runs in.
For Java) You can set the CLASSPATH environment variables by adding the following line. For example,
export CLASSPATH=.:/usr/local/tomcat/lib/servlet-api.jar
Take note that Bash shell uses colon (:) as the path separator; while windows use semicolon (;).
Step 3: Add the following line to the end of the file adding whatever additional directory you want in your path, for example:
export IM_STUDIO_PATH=”$HOME/.imstudio/bin:$PATH”
// Append a directory in front of the existing PATHexport PATH=/usr/local/mysql/bin:$PATH
That example would add ~/.imstudio to the PATH. The $PATH part is important as it appends the existing PATH to preserve it in the new value.
Step 4: Save the .bash_profile file and Quit (Command + Q) Text Edit.
?use :wq! if using nano-editor?
Step 5: Force the .bash_profile to execute. This loads the values immediately without having to reboot. In your Terminal window, run the following command.
source ~/.bashrc// orsource ~/.bash_profilesource ~/.profilesource /etc/profile
That?s it! Now you know how to edit the PATH on your Mac OS X computer system. You can confirm the new path by opening a new Terminal windows and running:
echo $PATH
You should now see the values you want in your PATH.
(Notes) For the older csh (C-shell) and ksh (Korn-shell)
Use ?printenv” (or “env”) to list all the environment variables.
Use ?setenv varname value” and “unsetenv varname” to set and unset an environment variable.
Use ?set varname=value” and “unset varname” to set and unset a local variable for the current process.
Step 6: Last but not least if you make somethings wrong with your PATH, and you want to ?redo? or ?remove? wrong path, you can think about this way:
launchctl unsetenv IM_STUDIO_PATH
3. Beautiful Tips
3.1 ? Check the current environment variable settings.
type : ?printenv? in your terminal
3.2 – Example variables
export JAVA_HOME=$(/usr/libexec/java_home)export JRE_HOME=$(/usr/libexec/java_home)
3.3 ? How to Set JAVA_HOME in Linux/macOS (Bash Shell)
First, check if JAVA_HOME is already set by start a terminal and issue:
echo $JAVA_HOME
JAVA_HOME is to be set to the JDK installed directory. You need to find your JDK installed directory.
[TODO] find macOS and Ubuntu JDK installed directory.
Add the the following line at the end of “~/.bashrc” (or “~/.login”). Take note that filename beginning with dot (.) is hidden by default.
[TODO] How to un-hide for macOS/Ubuntu.
export JAVA_HOME=/path/to/JDK-installed-directory
You need to refresh the bash shell for the new settings to take effect. Issue a ?source” command as follows:
// Refresh the Bash Shellsource ~/.bashrc // or “source ~/.login”// Verify the new settingecho $JAVA_HOME
Thank for your watching. Please feel free to feedback for me.
- https://hathaway.cc/2008/06/how-to-edit-your-path-environment-variables-on-mac/
- https://osxdaily.com/2015/07/28/set-enviornment-variables-mac-os-x/
- https://www3.ntu.edu.sg/home/ehchua/programming/howto/Environment_Variables.html