Example How To Add Directories To PATH Variable In Linux

May 18, 2011 | By
| Reply More

The environment variables control the behavior of the shell. Environment variables are the variables that set the working environment of the shell. Some of the environment variables are USER, HOME, SHELL, PWD, SHELL, PS1, PS2, etc. The HOME variable contains the path of home directory of the user. Similarly, the other variables contain other values needed for the operation of the shell. This article discusses an important shell environment variable called PATH and how you can add values to this variable.

Display Variable Value

The shell interprets the value of a variable by the "$" sign. To display the value of a variable, precede the variable with "$" sign. The "echo" command is used to display the value of the variable. Let us display the value of the variable HOME:

$ echo $HOME
/home/raghu

The PATH variable

The commands in Unix/Linux are the binary executable files. When you enter a command at the shell prompt, the binary file with that name is executed. So when a command is entered at the prompt, the shell searches for that binary file in some directories. These directories are listed in the PATH variable. Without the PATH variable, no command can be executed. Now let’s check the current value of PATH variable.

$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

The directories in this variable are separated by : (colon). At present, the shell searches the following directories for the binary executable files:

• /usr/local/sbin
• /usr/local/bin
• /usr/sbin
• /usr/bin
• /sbin and
• /bin

The order of the search path is also important. Suppose, you enter the "ls" command at the command line. Now at first, the shell searches /usr/local/sbin directory. If it does not find an executable file with that name in this directory, then it looks in the /usr/local/bin directory and then /usr/sbin, followed by /usr/bin and so on. If none of the directories specified in the PATH variable contains that file, then "command not found" error is displayed on the terminal.
"which" command is used to find out the directory that contains a particular command. This command also uses the PATH variable to search for the command location. For example,

$ which ls
/bin/ls

It means that when you type ls at the prompt and execute the ls command, then “/bin/ls” file is executed. In other words, location of ls command is “/bin/ls”. You can also find path of other commands with "which" command.

Add Directories to PATH

Now assume that you create your shell scripts in bin directory located in your home directory, i.e. in “~/bin” directory (~ represents home directory); and now you want to add this directory to your PATH variable as well, so that you do not have to specify the path to your script every time you run the script.

The value of a variable is changed with the syntax “variable=value”. But here, we want to add a directory to the PATH variable. We can’t just write “PATH=~/bin” because it will overwrite all the previous values. We want to preserve the existing values contained in the variable. So, we use value of the variable, i.e. $PATH, for assigning new value. If it sounds confusing, don't worry, it will be clear with the following command:

$ PATH=$PATH:~/bin
$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/root/bin

The "/root/bin" directory (/root is root user's home directory) has been added to the path variable. Now you can execute your script as a command, without specifying the full path to script.

Filed Under : HOWTOS, LINUX HOWTO

Tagged With :

Free Linux Ebook to Download

Leave a Reply

Commenting Policy:
Promotion of your products ? Comment gets deleted.
All comments are subject to moderation.