[email protected]Office hours: Mon – Fri 9:00AM – 5:00PMLinkedInXFacebook
NexonHost
Netherlands Dedicated ServersAmsterdam · Equinix AM6Germany Dedicated ServersFrankfurt · Equinix FR4Romania Dedicated ServersBucharest · VoxilityAustria Dedicated ServersVienna · Interxion VIEBulgaria Dedicated ServersSofia · TelepointFrance Dedicated ServersParis · Interxion PAR — Marseille · Digital Realty MRS3Ireland Dedicated ServersDublin · Equinix DB2Italy Dedicated ServersMilan · Irideos AvalonPoland Dedicated ServersWarsawSpain Dedicated ServersMadrid · Equinix MD2United States Dedicated ServersAshburn · Equinix DC
All articles
Getting startedApril 12, 2023

How to Add a Directory to PATH in Linux

How To Add A Directory To PATH In Linux

This article shows how to add directories to your $PATH in Linux systems.

When you type a command on the command line, you’re basically telling the shell to run an executable file with the given name. In Linux, these executable programs like ls , find , file and others, usually live inside several different directories on your system. Any file with executable permissions stored in these directories can be run from any location. The most common directories that hold executable programs are /bin, /sbin, /usr/sbin, /usr/local/bin and /usr/local/sbin.

But how does the shell knows, what directories to search for executable programs? Does the shell search through the whole filesystem?

The answer is simple. When you type a command, the shell searches through all directories specified in the user $PATH variable for an executable file of that name.

 

What is $PATH in Linux

The $PATH environmental variable is a colon-delimited list of directories that tells the shell which directories to search for executable files.

To check what directories are in your $PATH, you can use either the printenv or echo command:

echo $PATH

The output will look something like this:

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

If you have two executable files sharing the same name located in two different directories, the shell will run the file that is in the directory that comes first in the $PATH.

 

Adding a Directory to your $PATH

There are situations where you may want to add other directories to the $PATH variable. For example, some programs may be installed in different locations, or you may want to have a dedicated directory for your personal scripts, but be able to run them without specifying the absolute path to the executable files. To do this, you simply need to add the directory to your $PATH.

Let’s say you have a directory called bin located in your Home directory in which you keep your shell scripts. To add the directory to your $PATH type in:

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

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

You can now run your scripts by typing the executable script name without needing to specify the full path to the file.

However, this change is only temporary and valid only in the current shell session.

To make the change permanent, you need to define the $PATH variable in the shell configuration files. In most Linux distributions when you start a new session, environment variables are read from the following files:

  • Global shell specific configuration files such as /etc/environment and /etc/profile. Use this file if you want the new directory to be added to all system users $PATH.

  • Per-user shell specific configuration files. For example, if you are using Bash, you can set the $PATH variable in the ~/.bashrc file. If you are using Zsh the file name is ~/.zshrc.

In this example, we’ll set the variable in the ~/.bashrc file. Open the file with your text editor and add the following line at the end of it:

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

Save the file and load the new $PATH into the current shell session using the source command:

source ~/.bashrc

To confirm that the directory was successfully added, print the value of your $PATH by typing:

echo $PATH

 

Conclusion

Adding new directories to your user or global $PATH variable is pretty simple. This allows you to execute commands and scripts stored on nonstandard locations without needing to type the full path to the executable.

The same instructions apply for any Linux distribution, including Ubuntu, CentOS, RHEL, Debian, and Linux Mint.

Keep reading
How to Implement Network Segmentation and Resource Isolation in Linux.
February 6, 2024

How to Implement Network Segmentation and Resource Isolation in Linux.

How to Implement Network Segmentation and Resource Isolation in Linux. Securing and managing networks in a Linux environment involves advanced practices like network segmentation and resource isolation. This guide explores these concepts, providing detailed instructions and examples for…

Read article
How to open or close ports in Linux.
February 6, 2024

How to open or close ports in Linux.

How to open or close ports in Linux. Effectively managing ports on your Linux system is crucial for both security and service accessibility. This comprehensive guide will provide a detailed walkthrough on opening and closing ports using the robust tools iptables and ufw. Understanding these…

Read article
How to use ypdomainname command.
January 23, 2024

How to use ypdomainname command.

How to use ypdomainname command. In the realm of Linux networking, the ypdomainname command plays a crucial role in the context of the Network Information Service (NIS). NIS, formerly known as Yellow Pages, is a distributed database service that facilitates the sharing of network configuration and…

Read article
Get in Touch

Together, Let’s Build a Faster, Safer Internet.

Build scalable infrastructure with NexonHost — high-performance dedicated servers, VPS hosting, colocation, and DDoS protection across Europe.

Get Started →Contact Us
[email protected]Office hours: Mon – Fri 9:00AM – 5:00PM