The PATH Environment Variable
Adding Executable Program Commands to the PATH variable
Understanding the PATH variable and adding commands to use in your terminal
PATH variable
Your computer (Mac or Linux, or a Unix-based system) has an environment variable called PATH
, which contains a set of executable program directories that contain the executable programs.
Executable programs are basically the commands you can use in the shell.
These include the essential boot-stage or early-stage required binaries as well as other general system-wide commands.
As per the Filesystem Hierarchy Standard (FHS), these commands are located hierarchically in the system as follows:
In addition to the system-level binaries, executable programs can also include host specific program commands.
When you install a program or application on your Mac through the internet, the application will be available to execute using GUI, usually from the /Application
directory.
In order to use the command line command to run the application, however, the executable file for the application must be saved to the PATH
variable. In other words, you must add the directory of the executable program file for the application to the PATH
variable in order to use the name of the executable file as the command to run it from the shell.
When you install an application through a package manager, such as Homebrew, it is symlinked to /usr/local/bin
, which is usually included in PATH
.
For other direct program installations, it is your job to either save the original executable file to the PATH
variable directly, or symlink the executable file to a separate bin
folder, which would be included in PATH
.
Let’s take a look at how we can go about adding these local application commands to the PATH
variable so that we can use their commands in our shell.
Adding to PATH
In your zsh
shell profile (.zshrc
), you can add the following:
export PATH="/path/to/app/executable/file/directory:$PATH"
or
path+="/path/to/app/executable/file/directory"
export PATH=
export PATH="/path/to/app/executable/file/directory:$PATH
This syntax prepends
/path/to/app/executable/file/directory
to the existingPATH
variable.
export
command allows all child processes to inherit the marked variable.$PATH
refers to thePATH
variable value- assigning the path
/path/to/app/executable/file/directory
to thePATH
variable with the trailing:$PATH
essentially adds the path to the front of the existingPATH
value with the separator:
- You can also append the path to the existing
PATH
(add to the end) by instead usingexport PATH="$PATH:/path/to/app/executable/file/directory"
path+=
path+="/path/to/app/executable/file/directory"
path
is another variable that is tied to thePATH
variable, but it is an array.PATH
andpath
are tied together, so changing either one will change the other.
- however the variables’ value syntax are different:
>> echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/path/to/app/......>> echo $path
/usr/local/bin /usr/bin /bin /usr/sbin /sbin /path/to/app/......
- Note the
PATH
is separated by:
whilepath
is separated by whitespace. - You can force the
path
variable to have only unique values by usingtypeset -U path
command beforepath
assignment. This will keep thepath
value clean by preventing duplicate directory names being added.
Here, the path/path/to/app/executable/file/directory
will likely be a application bin
directory starting from the /Applications
directory.
- For example, for Visual Studio Code, the path is:
/Applications/Visual Studio Code.app/Contents/Resources/app/bin
- So, you would set:
export PATH="/Applications/Visual Studio Code.app/Contents/Resources/app/bin:$PATH"
This allows you to use the ‘code’ command (which is the name of the executable file) to run Visual Studio Code from the command line.
Symlinking the executable program file to a personal bin
directory
Instead of adding each executable program file to the PATH
separately, you can symlink the executable program file to a separate folder and add this folder to the PATH
.
- This separate folder could be a
bin
folder on your home directory:~/bin
. - You will have to create this in your home directory:
>> mkdir ~/bin
You can use ln
command to symlink the executable file.
ln
is a utility program that creates a new directory entry (a linked file), which has the same modes as the original file. The link 'points' to the original copy. How the link 'points' to the original file is the difference between a hard link and a symbolic link.- By default
ln
creates hard links, where any changes to the original file are effectively independent from the linked file. - Using the
-s
flag creates a symbolic link (symlink), which is a soft copy, allowing for the use of the referenced file when an operation is performed on the linked file.
>> ln -s "/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code" ~/bin
If you have the ~/bin
directory added to the PATH
variable, you just need to to symlink any executable program you want to add command for to the ~/bin
directory.
This will make the organization of the
PATH
much cleaner and much easier to see what executable programs are included in thePATH
.