PATH is a variable that allows you to run your scripts by simply typing its name without needing to specify the path to the command, let’s see what that means.
When you type a command on the command line, you tell the shell to run an executable file with the given name, In Linux these executable programs like ls, find,, are usually in several different directories on your system, All file with execute permissions (–x) stored in these directories can be executed from any location.
The most common directories that contain executable programs are: /bin, /sbin, /usr, /sbin, /usr, /local, /bin /usr, /local and /sbin.
But how does the shell know which directory to look in ?
The answer is simple, When you type a command, the shell searches all the directories I specified previously and specified in a variable named $PATH.
If you type a command whose location is not specified in the $ PATH variable, the shell will tell you that this command cannot be found and you will then have to type the command with its path.
In this guide we will see how to add a directory to the $PATH variable on Linux.
What is $PATH ?
The $PATH variable tells the Shell the directories to look for in order to find the program for the command typed.
To display the contents of the PATH variable, use the echo command :
# echo $PATH
Add a directory to $PATH :
In some cases you may want to add other directories to the $PATH variable.
For example, some programs can be installed in different locations or you want to have a dedicated directory for your personal scripts and you want to run them without specifying the absolute path to these executable files.
To do this, you just need to add the directory to your $PATH.
Assuming you’ve created scripts and put them in the scripts directory located in your home directory, here’s how to add that directory to the $PATH variable :
# export PATH="$HOME/Script:$PATH"
You can now run your scripts by simply typing the name of the executable script without having to specify the full path of the executable.
However, this change is only temporary and valid only in the current shell session.
To make the changes permanent, you must set the $PATH variable in the shell configuration files.
The environment variables are read from the following files :
–Global configuration files such as / etc / environment and / etc / profile. Use these files if you want the new directory to be added to all users on the system.
-Configuration files per user, For example, if you are using the Bash shell, you can set the variable $PATH in the ~/.bashrc file, and if you are using Zsh the file name is ~ /.zshrc :
# vim ~/.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
Show the value of $PATH to make sure the folder has been added :
# echo $PATH