Setting the Path Variable in OSX

Tag: 

Path definition
A path is a unique location to a file or a folder in a file system of an OS. A path to a file is a combination of / and alpha-numeric characters.

Why set a PATH variable?
A PATH variable is a system or environment variable used to store binaries/commands location/folder so that we don't need to type complete paths to execute the commands/binaries. If the PATH variable is not set, you may get errors like "Command not found". A PATH variable can store multiple folders names using a colon (:) as a separator.

How to list your current paths.

To list your current paths open the shell (terminal) and type the following command at the shell prompt.

echo "$PATH"
or
 printf "%s\n" "$PATH"

How to set the $PATH variable.

Bash is the default shell for MAC OS so unless you've installed a different shell we'll be using the Bash shell.

To modify your path, edit the $PATH variable. Open the shell (terminal) and type the following in your shell.

export PATH=$PATH:/path/to/dir

echo "$PATH" (to see if the variable was added)

Note that this only sets the variable for the currently open session. Once you exit the shell, the path is deleted from your environment.

How to make the PATH variable permanent.

When you set the $PATH variable in the .bash_profile file located in ~/, every time you open a bash shell the .bash_profile file is read and used to set up your environment.

First, check to see if you even have a .bash_profile.

  1. Open a bash shell
  2. Type: cd
  3. Type: ls -al and check if the .bash_profile is there.
  4. If there is not a .bash_profile listed you'll need to generate it
    1. Type: touch .bash_profile
  5. Type: ls -al and you'll now see a .bash_profile file.
  6. Type: echo 'export PATH=$PATH:/path/to/dir/' >> ~/.bash_profile
       Don't forget to include the single quotes.
  7. echo "$PATH" to see if the variable was added
  8. You could also open the file to see what's in it
    1. Type: less .bash_profile
    2. Type: q to exit the less program

Notes:

  • Each PATH is separated by a colon (:).
  • The $PATH at the end of the command appends or keeps previously assigned PATH variables so don't forget to add it.

References:

  1. cyberciti.biz