DEV Community

Mahendra Patel
Mahendra Patel

Posted on

$PATH Environment Variable in Linux

If you have downloaded some custom binary (external program) and want to access it from terminal irrespective of your current working directory, probably you are looking for PATH variable in Linux.
PATH is an environment variable which contains a list of directories separated by :.
When I typed echo $PATH it shows me

/home/my_user/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
Enter fullscreen mode Exit fullscreen mode

Oh, wait! But what are these folders, and what is their use?
These are the folders which contains binaries of programs installed on my machine.
Output for echo $PATH may be different for you based on your Linux distros and program you have installed.

So whenever I typed any command in the terminal, OS will start finding that program one-by-one into these directories, whenever it found first match, it will open it. If it can't find the given command it will display an error command not found.
Okay now that make sense!
But you have not answered my original question till now? I have installed a new program and I want to access it using terminal from any working directory. How can I do that?
A simple solution for this is to add the path of your new program to the environment variable $PATH.
Let's do it with an example.
I have recently downloaded flutter package and unzip it. But when I typed flutter I can't access it. To get this working I have to update the $PATH variable, something like this

export PATH="$PATH:/home/my_user/flutter/bin"

and bingo. Now if you type flutter in terminal you can access it from anywhere.
Are these changes are permanent, what if I close the terminal or shut down my machine, will they persist?
And the answer is NO.
Okay! So what is the permanent solution?
Permanent solution is update path variables via updating terminal profile/settings, I am using bash shell so to update bash setting I will update .bashrc file, If you are using zsh or any other terminal please update its respective profile/settings file.
Use the same command

export PATH="$PATH:/home/my_user/flutter/bin"

and add it to the end of the .bashrc file and save the file.
Now these changes are permanent even if you shut down or restart your machine.

So what we have learnt in this Post

  1. What is $PATH variable?
  2. Update terminal settings file, for bash it is .bashrc
  3. How to access any program from any working directly in the terminal.

Top comments (0)