
When you want to run a Linux tool from a non-standard directory, you need to add that directory to your $PATH
. In Linux most common executable like ls, find, file and more are found in /bin
, /sbin
, /usr/sbin
, /usr/local/bin
and /usr/local/sbin
directories. Any executable file stored in these directories can run from any location. It just so happens those directories are a part of what is called the user $PATH
. In this tutorial we will learn how to add directory to your PATH in Linux systems.
Once 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
is a list of directories that inform the shell that in which directories to search for executable files.
To see what’s in your $PATH
right now, type this into a terminal:
echo $PATH
It will print something like following:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
When you have multiple executable files having the same name and located in different directories then shell will consider the first directory which comes first in the $PATH
.
Adding a Directory to your $PATH
Sometimes you have requirement to run your executable files on shell without specifying the absolute path. To do this you just need to add the directory to your $PATH
.
For example, you have a directory work
located in your home
directory and a executable file is stored in it. Run the following command to add the directory to your $PATH
:
export PATH="$HOME/work:$PATH"
In above command we are using the export command to add a directory to the $PATH
. The directory will be included in the list of file system locations where shell searches.
You can now run your scripts without specifying the full path of executable file. Simply type the executable script name.
Remember that, this change is temporary change and valid only for the current shell session.
To convert the change in permanent, you need to define the $PATH
variable in the shell configuration files.
Adding directory permanently to your $PATH
will depend upon which shell you use. Generally, Linux distributions are running Bash. You can know your Shell by issuing the echo $SHELL
command.
For example, if you are using Bash, you have to set the $PATH
variable in the ~/.bashrc
file and if you are using Zsh the file name is ~/.zshrc
.
In this example, we are setting the variable in the ~/.bashrc
file.
Edit the file with your text editor and append the following line at the end of file:
nano ~/.bashrc
export PATH="$HOME/work:$PATH"
Save and close the file.
Next, close and re-open your terminal and type echo $PATH
command to check the entered path.
echo $PATH
Conclusion
It is very simple and straightforward to add new directory to PATH variable. Steps are same for any Linux distribution, like Ubuntu, CentOS, RHEL, Debian and Linux Mint.
If you have any question or suggestion, please leave a comment below.
Leave a Reply