how to append and set your path variable in linux

PATH or the path variable as it is commonly known, is an environment or system variable in operating systems, such as Unix or Linux that defines a set of directories in a specific order. These directories often contain executable programs and is used by the OS to search through in order to find and execute the most appropriate or relevant command. Each executing process and user session in a operating system has its own path variable.

The OS searches through each of the directories in the specified order to find the first available executable program or a file. This order of execution can only be overridden by either specifying the absolute path when executing the command or by modifying the path variable itself.

There are several reasons why you might want to update or append to your path variable. Some of the most common reasons are:

Installing new software: You installed a new software and would like to invoke it by directly using the program name, rather than the complete or absolute path to the executable file. This will require you to add the directory where the executable resides to the path variable.

Different versions of the same software: If you have different versions of the same software (such as java for example) installed and would like to use a particular version as the default. You can control which version get executed by ordering the directories in the path variable.

Custom scripts: It is common to have custom scripts that perform one or more commonly used user specific tasks. You can keep all your custom scripts in a single folder and then add the path to the folder in the path variable.

Convenience: This basically avoids the need to type out lengthy absolute paths to the executable and definitely the need to remember where a particular executable is installed. Typing java at the command prompt is much easier than having to type /opt/sun-jdk-1.6.32/bin/java.

You can set the path variable just as you will set any other variable in a Linux environment. As it is pretty much important that you have a default or working path variable for your system to run reliably, we will mostly look at how to append to the existing path variable rather than setting it.

Before you append or modify the path, it is worthwhile to check what the current value is. There are a couple of easy ways to check this, the simplest is to use echo to printout the variable

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

Another method is to use either the env or set command to display all the environment variables and then use the grep command to show the path.

bash$ env | grep -e ^PATH

or

bash$ set | grep -e ^PATH

When adding a new directory to an existing PATH variable, you can pretty much add it to any position in the path. Usually, it is added as a prefix (to the start of the order) or as a suffix (to the end of the order). Adding it to the middle or anywhere else is a little more involved if you need to do dynamically from the command line.

In order to add a new folder named sample (with an absolute path : /opt/sample) as a prefix, use the following syntax

bash$ export PATH=/opt/sample:$PATH

This will add the folder as the first in the path order and then append the existing value of the PATH variable to the end of it.

In order to add this folder as a suffix to the path, use

bash$ export PATH=$PATH:/opt/sample

If you want to put the folder in the middle (ie. anywhere other than the start or end), then you have to type out the entire path by yourself. From the previous example, if you like insert the folder /opt/sample between /sbin and /bin, then the cut/copy and paste functionality is your best friend.

bash$ export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/opt/sample/:/bin:/opt/bin

However or wherever you add the directory, be sure to preserve the existing value of the variable, unless of course you don’t want to.

If you like to use the modified path variable only in the current shell, then you should not export it. This will make sure that the current updated path variable is used only in the current shell.

As with other environment variables, you can export the path variable in order to update it for the entire session. This will make sure that the updated variable is available to all subsequent shells in addition to the current one. This will last only for the current session though and will be lost on reboot or logout.

If the updated path needs to be made much more permanent and is to be made persistent across sessions, then you will need to define the change in the shell configuration file (bashrc or the cshrc or other depending on the shell).

All users can update his or her’s environment path variable. Where and when this path variable can be used depends on the permissions available to the user. The superuser can change the default path variable of all users, but this default path can then be overridden by the user.

PATH is nothing but an environment variable like any other, but used for specific purposes by the operating system. You can see how you can view, edit and delete environment variables which also holds true for the PATH variable.