Jun 22, 2023 4 min read

How to Add a Directory to $PATH in Linux

Add a directory to PATH in Linux with our step-by-step tutorial. The $PATH is a crucial concept in computer systems.

Add a Directory to $PATH in Linux
Table of Contents

Before we begin talking about how to add a Directory to $PATH in Linux, let's briefly understand – What is $PATH?

The $PATH is a crucial concept in computer systems. It is an environment variable that contains a list of directories where the operating system searches for executable programs. When you type a command in the terminal, the system looks for the program's binary file in the directories listed in the $PATH.

By properly setting the $PATH variable, you can ensure that the system can locate and execute programs effortlessly. Understanding $PATH is vital for effective navigation and execution of commands in the terminal.

In this tutorial, we'll teach you how to add a Directory to $PATH in Linux. we will also address a few FAQs on how to add a directory to $PATH in Linux.

Advantages of  $PATH

  1. Simplifies command execution: $PATH allows you to execute commands from anywhere in the system by specifying the program name without needing to provide the full file path.
  2. Flexibility and scalability: Easily add or remove directories from $PATH to accommodate new programs or update existing ones.
  3. Time-saving: Avoid typing long file paths repeatedly and execute commands quickly.
  4. Customization: Personalize $PATH to prioritize directories, ensuring specific versions or customized programs are executed.
  5. Scripting convenience: $PATH enables the execution of scripts by simply calling their names, increasing scripting efficiency.

What is $PATH in Linux?


The shell is instructed where to look for executable files by the $PATH environmental variable, which is a list of directories separated by colons.
You can use the printenv or echo commands to see which folders are included in your $PATH:

echo $PATH

You will get an output like below:

Output

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

Adding a Directory to your $PATH


In some circumstances, you might want to include additional directories in the $PATH variable. For instance, you might want to have a special directory for your own programs or have some programs installed in multiple places, but you still want to be able to launch them without giving the exact path to the executable files. You only need to add the directory to your $PATH to accomplish this.

Assume that your shell scripts are kept in a directory called bin that is situated in your Home directory. Type in the below command to add the directory to your bin.

export PATH="$HOME/bin:$PATH"

The updated variable will be exported to the shell child process contexts using the export command.

Now, instead of having to enter the complete path to the file, you can only type the name of the executable script to launch your scripts.

This modification only applies to the current shell session and is only temporary.

You must define the $PATH variable in the shell configuration files in order to make the change permanent. Environment variables are read from the following files when a new session is started in the majority of Linux distributions:

  • Global configuration files for the shell, such as /etc/environment and /etc/ profile. If you want the new directory to be added to every system user's $PATH, use this file.
  • Individual shell configuration files for each user. For instance, you can modify the $PATH variable in the /.bashr file if you are using Bash. The file is called /.zshrc if you are using Zsh.

We'll set the variable in the .bashrc file in this case. With your text editor open, insert the following line at the end of the document:

Using the source command, save the file and import the new $PATH into the active shell session:

nano ~/.bashrc
export PATH="$HOME/bin:$PATH"

Print the $PATH value by typing the below command to verify that the directory was added successfully.

echo $PATH

FAQs to Add a Directory to $PATH in Linux

Can I add multiple directories to PATH?

Yes, you can add multiple directories to PATH by separating them with a colon (:) character. For example, PATH=$PATH:/path/to/dir1:/path/to/dir2.

Do I need to restart the terminal after adding a directory to PATH?

No, you don't need to restart the terminal. Simply source the updated .bashrc or .bash_profile file using the command source ~/.bashrc or source ~/.bash_profile for the changes to take effect.

How can I verify if the directory is successfully added to PATH?

After adding the directory to PATH, you can use the echo $PATH command to display the current PATH variable and confirm if the directory is included.

Can I temporarily add a directory to PATH?

Yes, you can temporarily add a directory to PATH for the current session by using the command export PATH=$PATH:/path/to/directory. It will not persist after the terminal is closed.

How can I make the PATH changes permanent?

To make the PATH changes permanent, you need to add the appropriate command (e.g., export PATH=...) to your ~/.bashrc or ~/.bash_profile file and then save the changes.

Is it possible to remove a directory from PATH?

Yes, you can remove a directory from PATH by modifying the ~/.bashrc or ~/.bash_profile file and removing the corresponding path from the PATH variable.

Are there any security considerations when adding directories to PATH?

Yes, be cautious when adding directories to PATH. Ensure that the directories you add are trusted and don't inadvertently expose your system to potentially malicious scripts or programs.

Conclusion

It's not difficult to add new folders to your user's or the world's $PATH variable. This eliminates the need to type the complete path of the executable when running commands and scripts that are stored in unusual locations.

Any Linux distribution, such as Ubuntu, CentOS, RHEL, Debian, and Linux Mint, follows the same procedures.

If you have any queries, please leave a comment below, and we’ll be happy to respond to them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.