What is PATH Environment Variable

In this article, I have explained about the PATH environment variable with proper examples and what is the advantage of using this in any operating system.


Table of Contents:

  1. What is Environment Variable?
  2. What is Environment?
  3. What is PATH Environment Variable?
  4. Usecase of PATH Environment Variable
  5. Real Life Example
  6. Resetting the PATH

Before understanding the PATH environment variable specifically, we need to understand what are environment variables in general and why do we even need those.

#What is Environment Variable?

Each computer has some operating system and there are multiple applications running on that operating system. These applications communicate with other applications and the operating system. Then they might need some small information for communication itself.

In other scenario, the application might require some information to run itself.

These small information is kept in the form of environment variables. We will understand shortly what type of informations are required by our applications.

These environment variables are used to keep and fetch information about the current environment.

Now you will ask, what is the meaning of environment here?

#What is Environment?

The environment is the context and our system’s condition in which the application is running.

For example: For us (humans) the environment will be our home, neighbourhood, office colleagues and other people around us. These things are considered as our environment since they impact us.

Similarly, for a running application the environment will be all those things which can impact and affect the application.

Let’s say, there are 2 computers running independently then the environment for both the computer systems will be different.

In another scenario, for 2 user’s accounts configured on a same system. In that also, the environment will be different for 2 applications running for 2 different users. Although, there can be some impact if something changes at the system level but if something is changing for one user, it won’t impact the other user and running application on that user. So the environment is different for two of these users.

So, the environment variables represent the combination of settings, configurations, and resources that affect the behaviour and functionality of software running on the operating system. These variables are a set of dynamic values which store details like file locations, system preferences, or user-specific information. Applications read these variable’s value and accordingly they behave in that environment.

#What is PATH Environment Variable?

Now, let’s talk about PATH variable. PATH is one of the important environment variable. It specifies directories to search for executable files.

You can see the current value of PATH variable using below command:echo $PATH

You should see output something like below image reference:

echo path command

So, the PATH variable contains the list of directories separated by colons.

#Usecase of PATH Environment Variable

Now, let’s understand what is the use of PATH variable and when we should use this?

If I talk about Linux machine, you would have surely used ls command. If we invoke ls command from any terminal, it lists down all the files of the current directory. But, have you wondered why it is able to do so, how that terminal is able to perform that operation and how does it know what it has to do once you type ls command?

Now, if you go to /bin directory then you will find one file with the name ls there.

Here is the screenshot of that (for you if it is not present in /bin, then it will be inside /usr/bin or /usr/local/bin folder):

bin folder content

So, this is the actual executable file which gets executed each time you hit ls command.

But, now the question is: how it is able to find out the path of this executable file?

The answer is PATH variable : Since we have already defined this path in our PATH variable.

Now, if you run below command, you will see the /bin directory in the output string.echo $PATH

Here is the image reference (I have highlighted the /bin directory in image):

echo $path command with /bin highlighted

Now, if you execute /bin/ls command in any terminal. Then also you will get the same output. Since, now you are just giving the full path of the executable file.

Now, let’s remove the /bin folder from the PATH variable. After editing the PATH value, run the below command:

NOTE: Don’t copy this directly, this value is relevant for my system. You have to remove /bin: in your PATH variable’s value and keep other things as it is.PATH=/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin

path set command after removing bin folder

Now, if you try to run ls command again. You will get error zsh: command not found: ls.

ls not found error

But, if you run /bin/ls command, you will get the expected output properly.

run ls after giving full path

Now I guess, you would have got the idea why it was working fine earlier without giving the full path of ls.

So, we use PATH variable for all those scenarios where we don’t want to give entire path each time to run our binary executable files and we want to run those from any location in our system.

So, whenever we type any command it fetches the value of PATH variable and then serially searches for that particular binary executable file in each mentioned directory.

#Real Life Example

You must have used maven sometime in your any of the project and did the installation for the same.

If you haven’t done yet you can try now along with this example, for that you can follow¬†maven installation guide.

So, after downloading and unzipping of the entire maven binary folder, it is required to set the PATH variable to add the bin directory of the maven. The reason there is also same.

Since, we don’t want to give full path of mvn binary each time whenever we are building any project. So, better to add the path in the PATH variable and rest is left to our operating system. Which will automatically reach to this location and execute the mvn command.

#Resetting the PATH

Let’s add /bin directory again in the PATH variable, so that our ls command works again without giving full path.PATH=$PATH":/bin"

Refer to following image for reference:

resetting path variable after practicle example

I hope, you would have understood the concept of PATH environment variable after reading this article. You can write us using Contact Us page for any further assistance.

Leave a Comment