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

Unix PATH Variable

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.


Assign a new value to the PATH variable

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.

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.

PATH versus path difference

The PATH variable is sometimes confused with the term path in UNIX. A path is used to indicate the location of a file or a directory. So, the path to the ls file in the
/usr/bin directory is /usr/bin/ls. 

The path to the passwd file in the /etc directory is /etc/passwd.
PATH is a variable used to help the shell construct a path to the command you are running. Let us say your PATH variable has the following value:
/usr/bin:/export/home/susan

PATH is list of Directory Names

PATH is a list of directory names separated by colons. When you type in the ls command, the shell looks through each directory in your PATH for a file called ls. It finds an ls file in the /usr/bin directory. The shell then composes the path /usr/bin/ls and then runs your command. This happens in the background, so you will not see the shell do this.
Notice that in the example above, I have included the /export/home/susan directory in the PATH. If I create a script in
/export/home/susan
called “myscript”, I can run it using the command:
$myscript

The shell sees this command and starts searching through the directories in my PATH variable.
It will not find a file called myscript in
/usr/bin, 

so it keeps looking. It does find a myscript file in /export/home/susan. Once the file is located, the shell creates the path
/export/home/susan/myscript

and runs the script.
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
When you want to modify the `PATH` variable in a startup file on UNIX, you typically choose from files like `.bash_profile`, `.bashrc`, `.profile`, etc., depending on the shell you're using and whether it's a login or non-login shell, and interactive or non-interactive session.
For this example, I'll show how to add a custom directory to the `PATH` variable in the `.bash_profile` file for a Bash login shell. If you use a different shell or want it to apply to non-login sessions, you might consider modifying `.bashrc` or `.profile` instead.
  1. Open your `.bash_profile` in a text editor. You can use `nano`, `vi`, or any editor you prefer. If you're using `nano`, the command would be:
    nano ~/.bash_profile
    
  2. Add the following line to the end of the file. This example adds `/usr/local/mycustombin` to the beginning of the current `PATH`. Adjust the directory path as needed for your specific case.
    export PATH=/usr/local/mycustombin:$PATH
    

    This command uses `export` to set the `PATH` environment variable, prepending `/usr/local/mycustombin` to the existing `PATH`. The `$PATH` at the end ensures that the original `PATH` content is preserved and appended after your custom directory.
  3. Save the changes and exit the editor. If you're using `nano`, you can do this by pressing `Ctrl + O`, `Enter` to save, and then `Ctrl + X` to exit.
  4. To make sure the changes take effect, you can either restart your terminal session or source the `.bash_profile` file by running:
    source ~/.bash_profile
    

Now, any new terminal session will include your custom directory in the `PATH`, allowing executables in that directory to be found and run without specifying their full path.

Korn and Bourne (.profile)

If your login shell is the Korn shell or Bourne shell, the shell will automatically run any UNIX commands listed in the file called .profile in your home directory during the login process. If you want to change your PATH variable, you must change the definition in this file. The new PATH definition will take effect the next time you login. The sample commands below would be added to your .profile file to add the current directory (the . directory) to an existing PATH.
 PATH=$PATH:.
export PATH

C Shell (.login)

If your login shell is the C shell, the shell will automatically run any UNIX commands listed in the file called .login in your home directory during the login process. The sample commands below could be added to your .login file to add the current directory (the . directory) to an existing PATH.
setenv PATH ${PATH}:.


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.

SEMrush Software 8 SEMrush Banner 8