Shell Processes   «Prev  Next»
Lesson 8 The PATH
Objective Describe the PATH variable.

Unix PATH Variable

Describe the purpose of the PATH variable in UNIX
The PATH variable in UNIX is a critical environmental variable that provides the operating system with a list of directories in which to search for executable programs and scripts. When you type a command in the shell, the system will use the directories listed in your PATH variable to find the corresponding executable file for the command.
The PATH variable holds its list of directories as a string, with each directory separated by a colon (:). By default, this string includes several standard directories, such as /usr/bin and /usr/local/bin, but users can add their own directories to PATH as needed.
The order of the directories in the PATH variable matters. When the system is searching for an executable, it goes through the directories in the PATH variable in the order they are listed. Therefore, if there are two executable files with the same name located in different directories, the system will execute the one found in the directory listed first in the PATH variable.
Manipulating the PATH variable is a common task when installing software, particularly software that is distributed as source code rather than as a binary package. After compiling the source code into an executable file, users will often need to add the directory containing the new executable to their PATH variable, enabling them to run the program from any location within the system by typing the executable's name into the shell.
In summary, the PATH variable in UNIX is a key component of the operating system's command execution process. It provides the system with a list of locations to look for executable files, thus allowing users to run programs from the command line without needing to specify the complete path to the executable file.
All the UNIX shells use a variable called PATH. This variable contains a list of directories that the shell will search when you type in a command. When you type in the ls command, the shell searches all the directories listed in your PATH until it finds a file named ls. It goes through each of the directories, in the order listed in the PATH, searching for your program.

Question: Which command do I use in UNIX to assign a new value to the PATH variable?
In UNIX, you can assign a new value to the PATH variable using the export command in the terminal. This command allows you to define environment variables and their values, which are then available to child processes spawned by the shell.
Here is the general syntax for setting the PATH variable:
export PATH=new_value

Replace new_value with the new directory path you want to add. Remember, each directory is separated by a colon (:).
For instance, if you want to add the directory /opt/myprogram/bin to the existing PATH, you would use the following command:
export PATH=$PATH:/opt/myprogram/bin

In this command, $PATH represents the existing PATH value, and :/opt/myprogram/bin is the new directory being added. The colon (:) acts as a separator between different directories in the PATH.
It's important to note that this change is only effective in the current session and will not persist across sessions or reboots. If you need to permanently modify the PATH, you'll have to edit a shell startup file, such as ~/.bashrc, ~/.bash_profile, or ~/.profile, depending on your shell and specific setup.
For instance, to make the change permanent for all bash sessions, you might add the export line to the ~/.bashrc file:
echo 'export PATH=$PATH:/opt/myprogram/bin' >> ~/.bashrc

This command appends the export command to the end of the ~/.bashrc file. After adding this line, you would need to run source ~/.bashrc to apply the change immediately, or simply close and reopen your terminal. Be sure to replace /opt/myprogram/bin with the actual directory path you want to add to PATH.
Colons are used to separate the directory names in the PATH.
This PATH contains two directories, /usr/bin and . (the current directory.)

script names and the PATH

When two programs have identical names, your PATH determines which one will run when you run the command.
For example, if you have an ls command stored in the /usr/bin directory and another version of ls stored in the /tmp directory you might run either version when you type in ls. If /tmp is listed first in your PATH, then /tmp/ls will run. If /usr/bin is listed first in your PATH, then /usr/bin/ls will run. When naming your scripts, you should choose a name that is not identical to a UNIX command. Usually the shell will locate the directory for the UNIX command in your PATH before the directory for your script. This means that the UNIX command will run instead of your script when you type in the identical name on the command line

Do not name your script “test” or “script”.
These are both the names of UNIX commands. When you attempt to run your script, the shell will run these commands instead. This is because the directories for these commands come first in your PATH, before your current directory. These file names are tempting; it’s natural to want to call your script “script”. Instead, try using related file names like test1 or script2.

The PATH and child shells

When a child process is created, all of the exported variables changed that from a glossary link to a sidebar link. If it is a glossary term as well, we should style the next instance of it in this paragraph as a glossary link.But the first instance seems to point to the sidebar, not to the glossary term.

ok

Review of Exported Variables

The PATH is an exported variable, so any shells you create from your login shell will be sent a copy of your PATH variable.

The PATH and startup files

Your system administrator will probably set an initial PATH for your account by creating a startup file in your home directory. The commands in these files are run automatically by UNIX whenever you login to your account.
You can change the value of PATH in your startup file
[[Examples of changing the PATH in a startup file]] .

Path Variable Exercise

Click the exercise link to practice setting the value of PATH and observe how this affects which command is run.
Path Variable - Exercise
The next lesson concludes the module.