Linux is very respectful of the file system and file organization. Typically, when you run a command such as Bash, ls, cd, and others, you execute the executable file associated with the command.
Most Linux commands have their executable files in specific directories such as /bin, /sbin, /usr/bin, /usr/local/bin, and /usr/local/sbin. The Linux system recognizes that these directories contain the executable for the commands you run using the path variable.
However, what if the executable you want to run is not in the directories above? What should you do then? The answer is pretty straightforward: you can add the path to your target directory to the $PATH variable
For example, suppose a directory is part of the $PATH variable. In that case, the Linux system will search for the executable associated with the command you run in all directories stored in the path variable.
This guide will teach you how to add a directory to the $PATH variable, allowing you to run executables in the specific directory from anywhere in the filesystem. Before we learn how to add directories to the path, let us understand the $PATH variable.
Understanding $PATH
The $PATH environment variable is a list of colon-separated directories where the Linux system searches for executable files every time you run a command in the terminal.
To view the directories that are part of the $PATH variable, use the printenv command as:
You will get an example output similar to the one shown below:
Each directory is separated by a colon.
It is good to note that the default values in the $PATH variable may vary depending on the distribution.
If, for example, you have two executables with similar names, but in different directories, the shell will execute the one in the directory that appears first in the $PATH variable.
How to Add A Directory to Path
There are several instances where you may need to add a directory to the $PATH variable. For example, you could have an executable installed in a different location or a dedicated directory for third-party executables.
To add a directory to the $PATH variable, we can use the export command. For example, let’s say you have a directory /custom/bin.
You can do:
Using the export command, we export the modified variable to the shell child process environments.
You can verify that the directory has been added to the $PATH using echo as:
You will get an output similar to the one below:
Unfortunately, adding a directory to the path using this method is temporary and only exists within the current session.
To make the path variable persistent changes, we can use the shell configuration files loaded at each shell startup.
In Bash, you can add the $PATH variable in the ~/.bashrc file. If you are on ZSH, you can add the $PATH in the ~/.zshrc file.
For example, to add the directory to the path in the .zshrc file, we can edit the file and add the entry as:
export PATH="/custom/bin:$PATH"
Once done, save the file and load the changes using the source command as:
Conclusion
As you have seen from this tutorial, using the $PATH variable, you can add custom directories to separate system executables from the custom ones while still running commands from anywhere in the filesystem.