How to Add a Directory to the $PATH Variable in Linux

Linux TLDR
Last Updated:
Reading time: 4 minutes

In Linux, whenever you want to execute any executable program or script, you need to jump into the directory and execute your program or script.

But when you execute system commands like ls, pwd, echo, apt, and nano, you don’t need to jump into any directory; you can run them from anywhere, and any user can run them.

Why does this happen?

The answer is pretty simple; whenever you execute any commands in your system without specifying the command’s relative or absolute positioning, the shell will search for the specified command in the directories specified in the $PATH variable, and then it will look for the current directory.

So, whenever you execute the ls or apt commands, the system will search for them in the directories specified in the $PATH variable and return you the output.

Now the question is: what is the $PATH variable?

The $PATH environment variable is a colon-delimited list of directories that tells your shell where to first look for the specified executable program or script.

Execute the following command to get the list of directories specified in your $PATH using the printenv or echo commands:

$ echo $PATH

Output:

Checking the existing path to PATH variable

As you see above, this list of directories is where the commands you specified will be searched first.

The following is a list of common directories that hold executable programs or binary files:

  • /bin
  • /sbin
  • /usr/bin
  • /usr/sbin
  • /usr/local/bin
  • /usr/local/sbin

Now, if your specified executable program or commands exist in this directory, you can execute them from anywhere; otherwise, you need to jump into your program directory to run them.

However, to avoid jumping into the program directory and executing them from anywhere, you can add the path of that program directory in the $PATH variable, so whenever you execute that program, you can directly execute it without jumping into the program directory.

Note that if you have two executable files (with the same name) in multiple directories, then Shell will run the file that comes first in the specified $PATH directories.

Tutorial Details

DescriptionAdding a Program or Script Directory to Your $PATH Variable
Difficulty LevelLow
Root or Sudo PrivilegesNo
OS CompatibilityUbuntu, Manjaro, Fedora, etc.
Prerequisitesexport, source
Internet RequiredNo

Adding a Directory to Your $PATH Variable

So, if you have freshly installed programs in your home directory or a directory full of scripts, you can add that directory to your $PATH variable to execute the executable program or scripts from anywhere without specifying the relative or absolute path.

For example, you have a “~/bin” directory in your home directory in which you keep your shell scripts.

Execute the following command to add that directory to your $PATH variable.

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

The export command will export the modified variable to the shell child process environments.

Execute the following command to verify the path is added to your $PATH variable.

$ echo $PATH

Output:

Adding custom path to PATH variable and then verifying it

Another example,

If your program or script directory is present in “/var/bin“, then use the following command to add it to your $PATH variable.

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

Verify the existence of the directory in the $PATH variable.

$ echo $PATH

Output:

Another example of adding custom path to PATH variable and then verifying it

So, you are now able to execute your executable programs or scripts from anywhere. However, this change is temporary and valid for the current session.

If you close your terminal or restart your system, these changes will be undone; to make them permanent, you can add them to shell configuration files.

Permanently Adding the Directory to Your $PATH Variable

To make this change permanent, you need to specify the command (ex: “export ...“) in the shell configuration file.

However, there are two types of configuration that most Linux distributions follow to look for environment variables.

  • Global shell configuration files are located at “/etc/environment” and “/etc/profile“. Assigning the modified $PATH in this file will make your executable program or script accessible to all users.
  • Specific user configuration files are located at “~/.bashrc” for Bash, “~/.zshrc” for ZSH, and “~/. config/fish/config.fish” for Fish.

For example, if you want to set the $PATH variable for a specific user, first find out your default shell.

$ echo $SHELL

#OR

$ echo $0

Output:

Checking current logged in user login shell

As you can see, the default is the Bash shell, so next edit the bash configuration file using your choice of text editor like Vim or Nano.

$ vim ~/.bashrc

#OR

$ nano ~/.bashrc

And add the command at the end of the file.

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

Output:

Setting $PATH variable in bash configuration file

Save and close the file, and load the new $PATH in your current shell environment using the source command.

$ source ~/.bashrc

To verify the change, check the existing directories in your $PATH variable using the following command:

$ echo $PATH

Output:

Refreshing user login shell and checking the current paths added in PATH variable

As you can see, the directory is added permanently to your $PATH variable.

In this case, if you want to remove the custom added path from your $PATH variable, simply edit the shell configuration file, remove the line, and source the configuration file to apply the changes.

Permissions Denied for Program or Script

After you added your directory to the $PATH variable, you got the following error while executing your program or script.

bash: /home/linuxtldr/bin/script.sh: Permission denied

Than make sure to give executable permission for your program or script using the chmod command.

$ chmod +x  /home/linuxtldr/bin/script.sh

Adding All Subdirectories Within the ~/Bin Directory

If you have classified your executable programs or scripts in a separate subdirectory in your parent “~/bin” directory.

You can add each of the subdirectories separately to your $PATH variable using the single command.

export PATH="$PATH:$(du "$HOME/bin/" | cut -f2 | tr '\n' ':' | sed 's/:*$//')"

Command Breakdown:

  • The “du” command will list all the information about the subdirectories on a separate line.
  • The “cut -f2” command will extract the second column (ex: name of the subdirectories).
  • The “tr '\n' ':'” command will add “:” after each line, joining them together.
  • The “sed 's/:*$//'” command will remove the last colon.

That was the end of the article.

If you have any questions, feel free to ask them in the comment section.

Join The Conversation

Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.