How to Add a Directory to PATH in Ubuntu 22.04


The PATH environment variable is the key player in finding and executing the commands/applications/programs. Whenever the command is run, the system searches for that command’s executable in the $PATH. If the executable’s address is found in the PATH variable, it is executed instantly, else it throws an error.

What if you have installed/added any program other than the directories mentioned in the $PATH? In such a case, the executable won’t work until you add the executable’s directory to the $PATH.

This post will address the method(s) to add a directory to the PATH in Ubuntu 22.04.

Execution Flow of Shell Configuration Files/Profiles

Before getting into the depth, you must be aware of the execution flow of the shell configuration files. These files/shell profiles are loaded by the system at a specific time. By understanding the shell profiles, you will be able to decide where to put the $PATH variable:

Interactive Login Shell

Shells that use password authentication. Like switching the user’s or logging into a new user. The execution flow refers to the interactive login shells only.

  • First, the /etc/profile is read.
  • Then, ~/.bash_profile.
    • If ~/.bash_profile is not available then ~/.profile.
  • If ~/.profile is not, then ~/.bash_login.

Interactive Non-Login

Creating multiple shell environments (adding more terminal sessions). That does not require login. The following flow refers to interactive non-login shells but applies to the login shells as well.

  • ~/.bashrc
  • ~/.bash_history
  • ~/.bash_login

Non-Interactive Shells

Running a script where the user does not directly run the content of the script on the terminal. The following shell profile is loaded at the time of non-interactive shells:

  • /etc/bash.bashrc

Note: If you have opened the interactive/non-interactive session through the “interactive login”, then all the interactive login profiles will be loaded as well.

How to Add a Directory to PATH in Ubuntu 22.04

A directory path containing the executable must be added to the PATH variable to be recognized by the terminal. You can add a directory to the $PATH temporarily or permanently. Here, we will list all the possibilities to add a directory to PATH.

Temporarily | Ongoing Session For the Current User Only

A directory can be added to the PATH for the current session using the export command in the terminal. The current session will apply to the terminal’s session only. If you launch a new terminal (close the current terminal session), the directory added to $PATH will be obsolete.

Syntax

export PATH="$PATH:<path-of-the-directory>" #For Regular Priority
export PATH="<path-of-the-directory>:$PATH" #For Higher Priority
  1. Regular Priority

Use the below command to add the directory with a regular priority (the new directory will be added at the end of the $PATH values):

export PATH="$PATH:/home/adnan/Downloads/Discord"

  1. Highest Priority

To prioritize the directory above all, write the directory path before the $PATH as shown below:

export PATH="/home/adnan/Downloads/Discord:$PATH"

What if you want to add the directory to the PATH variable permanently for the current or all the users? Here comes the purpose/usage of the Shell profiles. Let’s see how to do it:

Permanently | For the Current User Only

The PATH variable can be modified permanently for the current user as well. From the execution flow, you either need to modify the “~/.bash_profile”, “~/.profile”, or “~/.bash_login”. Keeping the execution flow in view, we will add the directory “$PATH:/home/adnan/exec”.

Open the file in the editor and add the export command with the path as follows:

export PATH-"$PATH:/home/adnan/exec"

Reboot the system to apply the changes and check the value of the $PATH variable:

echo $PATH

We have verified it by switching the user and the directory path does not exist after switching. Thus, it will be only applicable to the user who opened/edited the file:

Note: If the “.bash_profile” file does not exist, you can add it to the “~/.profile” file as well. Similarly, you can add the export command with the path in the “~/.bashrc” or “/etc/bash.bashrc” for interactive non-logins / non-interactive shells.

Permanently | System-Wide For All Users

The “/etc/profile” and the “/etc/environment” files refer to the system-wide changes in the $PATH. These files can be modified to add the directory to the PATH which is recognized by the whole system and all the users.

  • Modifying the /etc/profile | Recommended

You need to add the export command alongside the directory path to the “/etc/profile” file. Open the “/etc/profile” with root privileges:

sudo nano /etc/profile

Add the following line inside the file:

export PATH="$PATH:/home/adnan/Downloads/Discord"

Reboot to apply the changes. Let’s verify:

echo $PATH

Here you go! The directory path has worked for the other user as well:

Note: The “/etc/profile” file is executed/loaded whenever the login to the shell is detected (as Ubuntu requires login to the system, it will be loaded instantly after the login and apply to all types of shells/users).

  • Modifying the /etc/environment | Not Recommended

Modifying the /etc/environment file is not recommended. However, if you know the syntax of changing the PATH, then you can back up the original path file and make the changes in it.

First, create a backup of the file “/etc/environment”. Then, open the original file in any editor and add the path of the directory at the end or start of the PATH variable:

Reboot the system to apply the changes.

These are the possible ways to add the directory to PATH in Ubuntu.

Bottom Line

Adding a directory to $PATH means that you want to add one more executable location on your system. Any executable placed in that directory can be executed/launched from the terminal (irrespective of the PWD).

To add a directory to PATH for the current session (and the current user only), add the export command in the terminal directly. Moreover, if you want to add the directory to PATH permanently, add the export command with the path inside the files “/etc/profile”, “~/.bash_profile” or “~/.profile”. The selection of shell configuration files depends on the scope you want to achieve for the PATH variable.

This post has demonstrated the methods to add a directory to PATH in Ubuntu 22.04.

Print Friendly, PDF & Email
Categories