The $PATH variable is a crucial component on Linux and other Unix-like operating systems. It specifies a list of directories that hold various executables on the system and tells the shell where to look for these executable files.

On a fresh Linux installation, the directories included in $PATH ensure that most programs you run work faultlessly. However, there may be times when you might want to quickly run custom scripts or programs on your system.

In such cases, you need to set the $PATH variable appropriately before running those programs.

What Is $PATH in Linux?

As mentioned above, $PATH is an environment variable in Linux, which comprises a colon-separated list of directories that the Linux shell refers to when it receives a command or an exec call from a program.

Typically, the $PATH variable includes the /bin, /usr/bin, and the /usr/local/bin directories, with superuser getting two more entries, namely, /sbin and /usr/sbin. However, you can add new entries to your $PATH variable to conveniently launch scripts and programs from the terminal irrespective of the current working directory.

Why Would You Want to Set $PATH?

At large, most programs you'll ever run on your Linux system will work flawlessly, and your shell will not present any errors at the time of their execution. For instance, when you run the ls command, the shell runs it error-free without requiring you to specify the command's full path.

However, when you've to run a custom script, you can't declare the exec command as you did with the ls command before. Instead, what you'll have to do is either navigate to the script's directory (via terminal) and run the command from there, or, if you want the ability to run the command from anywhere on the system, specify the script's absolute path in the command to help the shell locate it.

Related: How To Change Directory in Linux Using the cd Command

While both these methods work well, they're not very efficient and can quickly make the entire process a tedious routine. An alternative (read efficient and effective) way to run programs from non-standard directories is to add their source directory to your shell's $PATH so that it knows where to find the script, and can, therefore, execute them quickly.

How to Set the $PATH Variable in Linux

Depending on what you want to achieve---whether it's a temporary setup to run a script only once or you plan on using the script on a regular basis---there are different ways to add a directory to your $PATH.

1. Setting $PATH Variable Temporarily

If you want to run a script or a program in just your current (active) session, you can choose to set a temporary $PATH variable. When you do so, you'll be able to execute that program with a command from anywhere on your system without having to include its full path in the command but only in the current session.

To set a $PATH variable temporarily, open the terminal and use the following syntax to add directories to the variable:

        export PATH=$PATH:/path/to/directory
    

For example:

        export PATH=$PATH:/home/smithy/scripts/
    

Do note that with this configuration, you can only run the program in your active session. When you set your $PATH variable temporarily, the system will reset it back to the default setting after you restart your computer.

2. Setting a Permanent $PATH Variable

If there's a program on your system that you're likely to use frequently, you must set the $PATH variable for it permanently. This will ensure that your shell remembers its directory even after a reboot.

Unlike the temporary solution, though, setting $PATH permanently involves a few extra steps: identify the shell you're using, edit the corresponding configuration file, and set the $PATH variable there.

To identify the shell on your system, run the following command in the terminal:

        echo $0
    

You should see the name of your active shell as the output. In most cases, this will be the Bash shell, unless you've changed it to Zsh, Ksh, or some other Linux shell.

Now, based on the shell you're using, you will have to edit its configuration file. Here's what file you need to edit based on your shell:

Shell Name

Configuration File

bash

~/.bashrc

ksh

~/.kshrc

zsh

~/.zshrc

csh

~/.cshrc

In the case of some other shell, check out its documentation to find out its configuration file name.

Once you've determined the file you need to edit, follow the steps below to set the $PATH variable. In this guide, we will be demonstrating how to set up the $PATH variable in the Bash shell.

  1. Open the terminal and enter the following command to open the .bashrc file. You can use any Linux text editor of your choice.
            nano ~/.bashrc
        
  2. Use the following syntax to set the $PATH variable.
            export PATH=$PATH:/path/to/directory/
        
  3. Save the file and update the current shell environment using source.
            source ~/.bashrc
        
  4. Type the following command to check whether the directory has been added or not.
            echo $PATH
        

If you entered the directory path correctly, it will reflect in your system's $PATH, and you should be able to run all the scripts inside the folder from any directory on your system.

Besides, Linux also allows you to set the $PATH variable permanently in a system-wide configuration so that every user on your system can access and execute the custom scripts present in the specified directory.

To set $PATH globally, you'll need to add the directory using the same command you used while adding temporary and permanent setup, but either in the /etc/environment or the /etc/profile file.

Open either of these files and append the path of the directory:

        nano /etc/environment
nano /etc/profile

Successfully Setting the $PATH in Linux

With the above steps, you can set your system's $PATH to look for scripts in directories that are, otherwise, unspecified.

For most cases, we recommend you to set the $PATH variable on your system permanently in a per-user configuration since it's the most efficient way, out of the two methods.

Also, do keep in mind that unless there's a specific program on your system that you want to access globally, you wouldn't need to set the $PATH variable in a system-wide configuration. The locate and find commands in Linux are a lifesaver if you are facing issues while finding the source directory to add to the $PATH variable.