Site icon NetworkHunt

How to Set $PATH in Linux?

How to Set PATH Permanently in Linux

In Linux operating systems, the $PATH variable plays a vital role. It tells the shell where to search for executable files when a command is issued. By defining a list of directories, the $PATH variable ensures that programs can be executed from anywhere in the system without needing to provide their full path. By default, a fresh Linux installation includes directories like /bin, /usr/bin, and /usr/local/bin in the $PATH. These directories ensure most common programs work correctly. However, if you want to run custom scripts or applications from any location, you need to modify the $PATH variable.

Table of Contents

  1. What is $PATH in Linux?
  2. How to Set the $PATH in Linux?
    • Method 1: Setting $PATH Temporarily in Linux
    • Method 2: Setting $PATH Permanently in Linux

What is $PATH in Linux?

When a command is entered, the Linux shell searches for it in the directories listed in the $PATH variable. The $PATH environment variable typically includes directories like /bin, /usr/bin, and /usr/local/bin. If you have administrative privileges (superuser), two additional directories, /sbin and /usr/sbin, are also included by default. If you want to run custom scripts or programs located in other directories, you must add those directories to your $PATH.

To see your current $PATH, you can use the following command in the terminal:

$> echo $PATH

How to Set the $PATH in Linux?

There are two primary ways to set the $PATH in Linux:

  1. Temporarily (for the current session)
  2. Permanently (for all future sessions)

Method 1: Setting $PATH Temporarily in Linux

Setting the $PATH variable temporarily means that the change will only be valid for the current session. Once you close the terminal or restart the system, the $PATH variable will return to its default setting.

To add a directory to the $PATH for the current session:

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

Example: If you want to add the directory where the Go programming language is installed, you would use:

$> export PATH=$PATH:/usr/local/go/bin

With this command, you can run any programs located in /usr/local/go/bin from anywhere in the terminal, but the change will only persist for the duration of the current session. After rebooting or opening a new terminal, the $PATH will return to its previous state.


Method 2: Setting $PATH Permanently in Linux

If you frequently use a particular program or script, it’s better to set the $PATH permanently. This ensures that the new directories are always included in your $PATH, even after a reboot.

To do this, you need to modify the appropriate configuration file for the shell you’re using.

1: Identify Your Shell

To determine which shell you are using, run the following command:

$> echo $0

The output will indicate your current shell. For most users, this will be Bash (though it may also show other shells like Zsh, Ksh, etc.).

2: Open the Configuration File for Your Shell

Depending on the shell you’re using, you’ll need to modify a user specific configuration file:

Here’s an example of how to configure the $PATH in the Bash shell.

  1. Open the .bashrc file using a text editor (e.g., nano):
  2. nano ~/.bashrc
  3. At the end of the file, add the following line to include the new directory in your $PATH.
  4. Use command – $> export PATH=$PATH:/path/to/directory
  5. Save and close the file (in nano, press CTRL + O to save and CTRL + X to exit).
3: Apply the Change

To apply the changes made to the .bashrc file, run:

$> source ~/.bashrc

This will reload the configuration and update the $PATH variable for the current session.

4: Verify the Changes

To verify that the directory was successfully added to the $PATH, run:

$> echo $PATH

This will display the updated $PATH, and you should see the new directory path listed.


Method 3: Setting $PATH Globally

If you want to set the $PATH variable system-wide (for all users), you can modify system-wide configuration files. This is useful when you want all users on the system to access custom scripts or applications.

To do this, add the directory to one of the following files:

  1. Open the file for editing. For example, to modify /etc/environment, use:
  2. sudo nano /etc/environment
  3. Add the directory to the $PATH line (e.g., :/usr/local/go/bin).
  4. Save and close the file.

Once you’ve edited one of these system-wide files, the $PATH will be updated for all users on the system.

By following these steps, you can configure the $PATH variable either temporarily for a single session or permanently for all future sessions. The permanent method allows you to easily run custom scripts and programs from anywhere on your system without needing to type the full directory path each time.


Exit mobile version