-->

Setting the PATH Environment Variable

Setting the PATH Environment Variable
Setting the PATH Environment Variable

When you enter an external command (see Chapter 5) in the shell command line interface (CLI), the shell must search the system to find the program. The PATH environment variable defines the directories it searches looking for commands and programs. On this Ubuntu Linux system, the PATH environment variable looks like this:

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

This shows that there are eight directories where the shell looks for commands and programs. The directories in the PATH are separated by colons.

If a command’s or program’s location is not included in the PATH variable, the shell cannot find it without an absolute directory reference. If the shell cannot find the command or program, it produces an error message:

$ myprog
-bash: myprog: command not found
$

The problem is that often applications place their executable programs in directories that aren’t in the PATH environment variable. The trick is ensuring that your PATH environment variable includes all the directories where your applications reside.

You can add new search directories to the existing PATH environment variable without having to rebuild it from scratch. The individual directories listed in the PATH are separated by colons. All you need to do is reference the original PATH value and add any new directories to the string. This looks something like this:

$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:
/sbin:/bin:/usr/games:/usr/local/games
$$
PATH=$PATH:/home/christine/Scripts
$$
echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/
games:/usr/local/games:/home/christine/Scripts
$$
myprog
The factorial of 5 is 120.
$
By adding the directory to the PATH environment variable, you can now execute your
program from anywhere in the virtual directory structure:
$ cd /etc
$$
myprog
The factorial of 5 is 120
$

Tip
If you want your program’s location to be available to subshells, be sure to export your modified PATH environment variable.

A common trick for programmers is to include the single dot symbol in their PATH environment variable. The single dot symbol represents the current directory:

$ PATH=$PATH:.
$$
cd /home/christine/Old_Scripts
$$
myprog2
The factorial of 6 is 720
$

Changes to the PATH variable last only until you exit the system or the system reboots. The changes are not persistent. In the next section, you see how you can make changes to environment variables permanent.

0 Response to "Setting the PATH Environment Variable"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel