Setting the Path Variable in Linux

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 type the following command at the shell prompt.

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

How to set the $PATH variable.

To modify your path edit the $PATH variable based upon the shell you use. The syntax for setting path is dependent upon your login shell.

For Bash, Sh, and Ksh type one of the following in your shell.

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

For tcsh or csh, type one of the following in your shell.

set path = ($path /path/to/dir1)
or
setenv PATH $PATH:/path/to/dir1

Note that this only sets the variable for the currently open session.

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 so you don't need continually set it for every session.

Type one of the following for your perspective shell. Or you could also open the .file with a text editor and set the path there.

For Bash shells: echo 'export PATH=$PATH:/path/to/dir' >> ~/.bash_profile  

For KSH/sh shells: echo 'export PATH=$PATH:/path/to/dir' >> ~/.profile

For tcsh or csh, shells: echo 'setenv PATH $PATH:/path/to/dir' >> ~/.cshrc

To verify new path settings:
Type: $ echo $PATH