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