How to Add a Directory to the PATH in Linux?
We are all familiar with the concept of the Linux terminal and the commands we can run in it. However, have you ever wondered how the system is able to locate the executable for that specific command?
Does the shell traverse the entire filesystem to locate the specified binary? What is you pipe through to multiple commands?
Although filesystem traversal may seem like a fairly simple choice, it is much more complex and would be very inefficient due numerous reasons.
So, when you type a given command, the shell searches through a set of pre-defined directories using the $PATH
environment variable.
In this tutorial, we will learn how you can add a specific directory to the $PATH
variable, allowing the shell to search for any binaries in the directory when executing a given command.
What is the $PATH Variable in Linux?
In Linux and other Unix-based systems, the $PATH
variable is an environment variable that stores a list of colon-delimited directories where the shell looks for executable files when a command is issued.
To check the list of directories in your $PATH
environment variable, you can use the echo
command as shown:
echo $PATH
You should see an output similar to the one shown below:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin
You can also use the printenv
command as shown:
printenv PATH
Adding Directory to Path
When a command is entered in the shell, the system checks each directory specified in the $PATH
variable, in order from left to right, to find the corresponding executable file. If a matching executable is found, the shell executes it.
The $PATH
variable is usually set in the shell’s configuration files, such as ~/.bashrc
or ~/.bash_profile
, during the initialization of the shell session.
Therefore, to add a specific directory to the $PATH
, you need to modify the configuration file for your shell.
For example, suppose you have a directory called scripts
in the /opt
directory where you wish to store custom binaries and scripts.
To add that directory to the $PATH
, you can use the export command:
export PATH="/opt/scripts:$PATH"
The export
command will update the shell child processes with the new values. This allows you to use the environment variables in that session.
However, this changes are temporary and will not persist across sessions.
To make the changes permanent, you need to configure the path in the shell configuration file. For example, if you are using Bash, edit the .bashrc
or the .bash_profile
files.
nano ~/.bashrc
In the configuration file, add the export statement for your target directory as:
export PATH="/opt/scripts:$PATH"
Save the changes and close the file.
To apply the changes in your current session, use the source
command:
source .bashrc
This should reflect the new changes and allow you to run any executables from the specified directory without using the absolute path.
Conclusion
In this tutorial, you discovered a simple and straightforward method of adding a directory to the $PATH
environment variable. This allows you to run executables from custom directories without using the full path to the target binary.
Leave us a comment down below for any changes, suggestions, or complaints!!