In Linux, $PATH is a system-dependent environment variable. This variable tells the shell where to find executable commands.
Setting the PATH variable in a Linux system provides greater flexibility and security by allowing users to set the path in various effective ways permanently.
The $PATH
that you specify impacts the Linux system in a critical way. The shell searches for the path to that command in the $PATH
variable when you execute any command. For instance, when you run an SSH command, it searches for the actual location of SSH in the $PATH
environment variable.
This article discusses several ways to utilize the $PATH
environment variable to provide flexibility to the Linux system.
Why is My $PATH Execution Giving an Error?
If you enter an external command in shell, for example, myprogram.c
, it gives an error as shown below:

It means that the shell is unable to find the command in the $PATH
variable. You can check the $PATH
variable by printing it using the echo command:
echo $PATH
You will see the output similar to the one given below:

The output lists all the directories added to the environment variable. All the directories are separated by a colon.
It is crucial to add a path to this environment variable when you execute any program. To check the path of a program, type and execute:
which useradd
You will see the following output:
/usr/sbin/useradd
And you can see this path already added in the $PATH variable.

In other words, if you set the $PATH
variable accurately, you can use various commands on Linux like the TCC compiler or Java JDK. However, if you do not set the path appropriately, the execution of that particular command will fail.
How to Resolve $PATH Error
Let’s look at one more example. Create a folder named scripts
using mkdir
command. Then, create a shell script file called test1.sh
.
Now, if you try to run this file, it will give the same error.

Let’s try setting this path to the $PATH environment variable. The syntax is:
PATH=$PATH:/new/directory/here
or
PATH=/new/directory/here:$PATH
The first syntax will add the path at the end of the $PATH
variable. Similarly, the second syntax will add the path at the beginning of the $PATH
variable.
Now, to set your path, type:
PATH=$PATH:/root/scripts
Hit enter and try executing the file again. It will run successfully. If you check the $PATH
variable, you will see that this new path exists in the $PATH
environment variable.
However, this will set the path temporarily. To permanently add the path to the $PATH variable, there are multiple ways. Please note that profile files vary according to the shell type. In bash shell, it can be ~/.bash_profile
, ~/.bashrc
, or .profile
. In Korn shell, it is ~/.kshrc
or .profile
. Similarly, in Z shell, it is ~/.zshrc
or .zprofile
.
Set PATH Variable Using .bash_profile
The first mistake that users often make is to set the path temporarily. It results in losing all the currently added paths in the environment variable. The first method to add the path permanently to an element variable is to store it in .bash_profile
. It will customize the path for the single user of the system.
To permanently set the PATH variable in Linux to .bash_profile
, edit the file using vi
, nano
, vim
, or emacs
command:
$ vi ~/.bash_profile
Set the path:
export PATH=$PATH:/path/to/your/directory
The command adds the current directory to the element variable and preserves the previous existing paths. If you do not add the $PATH element with the export command, you will lose all the previously existing paths, and the system might malfunction due to the missing variables.
To reload the changes, type the source command:
$ source ~/.bash_profile
You can also confirm that the path exists in the environment variable by echoing the $PATH
:
$ echo $PATH
If you are using Z shell or Korn shell, you can perform the steps in the profile files. Instead of .bash_profile
, the file would be ~/.zshrc
and ~/.zprofile
for Z shell. Similarly, it will be ~/.kshrc
and ~/.kprofile
for Korn shell. You can also use shells like C Shell or tcsh shell.
Set PATH Variable Using .bashrc
Instead of adding paths in .bash_profile
, you can also set paths to the directories in .bashrc
. Setting the path environment variable in bashrc
is similar to setting it in .bash_profile
. To add the path in bashrc
, type:
export PATH="$PATH:/home/user/.rbenv/bin"
The location to bashrc
file is: /home/user/.bashrc
The difference between bash_profile
and bashrc
is that bashrc
is specifically used for login via the console or login using SSH. Moreover, source
the changes as you did with bash_profile
by executing the command:
source ~/.bashrc
User-Wide Initialization
Another method is to set the user profile instead of the .bash_profile
. The changes will be for current users only. Depending on your shell type, you can add the files in .profile
, or .zprofile
.
The user profile file is located at /home/user directory
, or ~/.profile
.
Open .profile
file and type
# ~/.profile
export PATH=$PATH:/path/to/your/directory
Change “/path/to/your/directory” with the exact path that you want to set. Then activate the new $PATH
by using the source command.
$ source ~/.profile
System-Wide Initialization
System-wide settings for all users are available in the /etc/profile
. To add a path to the $PATH environment variable in a way that affects all the users, add the path in the /etc/profile/
. It will be visible to all the users of the system.
However, you must have root privileges to execute this step. To add the path, edit .profile
in the etc
directory:
# /etc/profile
export PATH=$PATH:/path/to/your/directory
This guide explains various methods by which you can add a path to your environment variable. You can set the PATH variable in Linux temporarily or permanently. You also have the flexibility to set the path user-wide or system-wide, and for different types of command shells.
In case, you encounter an error, better check $PATH first; you can check this using the command:
$ echo $PATH
However, users must not change path variables unless they know what they are doing. To learn even more about path variables, check the Wikipedia page.