Adding Executable Program Commands to the $PATH variable

Peter D Lee·2021년 3월 7일
0

Command Line

목록 보기
4/4
post-thumbnail

Installing Programs

  • Binary installation --> installs the pre-compiled program that is immediately executable in binary.
  • Source installation --> installs the source code of the program and needs separate compilation of the source code to be executable.

PATH variable

Your computer (Mac or Linux) has an environment variable called PATH, which contains a set of executable program directories that contain the executable programs for your computer.

Executable programs are basically all the commands you can use inside the shell.

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, 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.

Adding to PATH

In your 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 existing PATH variable.

  • export command allows all child processes to inherit the marked variable.

  • $PATH refers to the PATH variable value

  • assigning the path /path/to/app/executable/file/directory to the PATH variable with the trailing :$PATH essentially adds the path to the front of the existing PATH value with the separator :

  • You can also append the path to the existing PATH (add to the end) by instead using
    export PATH="$PATH:/path/to/app/executable/file/directory"


path+=

path+="/path/to/app/executable/file/directory"
--> path is another variable that is tied to the PATH variable, but it is an array. PATH and path are linked completely, 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 : while $path is separated by whitespace.

  • You can force the path variable to have unique values by using typeset -U path command before path+=... This will keep the path value clean by preventing duplicate directory names being added.

Here, the /path/to/app/executable/file/directory will likely be a bin directory starting from the /Applications directory.

  • For example, for VS 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"


Instead of adding each executable program directories 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 refererrenced 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 saved in the PATH.

0개의 댓글