In Linux, the PATH environment variable stores the names of paths that will be searched for the executable files of any commands typed in at the command line. The value of the PATH environment variable is a string containing a series of pathnames, each delimited by a colon. For instance, the default PATH on a typical system might look like this:
When you type in a command such as cat at the command prompt, your shell will look in each of these directories for an executable file named cat. The first one it finds is the one it runs.
To view the current value of your PATH environment variable, you can use the echo command. As with all variables in the shell, when referring to the value you need to put a dollar sign before the variable name:
In the above example, the current value of path return you to the command prompt.
Setting PATH for your current shell session
You can set the value of PATH as you would any other shell variable, with the form NAME=VALUE, like this:
The problem with this command is that it will completely overwrite the values you had before, which you probably don’t want. If you want to add a new value in addition to the old ones. You can accomplish this by referring to PATH in the new definition, like this:
Using the above command adds your new path to the current value of PATH. Since the pathnames are searched in order, you probably want to add your new path at the end of the variable as we’ve done here. If instead you typed:
Your new path would be searched before, not after, the default system paths.
Using export to pass the PATH environment variable to child processes
This type of PATH definition will set the environment variable for your current shell session, but any new programs you run might not see the new path you’ve added. That’s because your shell lets you control the environment by requiring you to manually declare what environment variables are passed on to other programs and processes. You can accomplished this with the export command. If you run:
Any processes you run until you log out will use the current value of PATH.
If you prefer, you can combine these two commands into a single line, for convenience. Just put a semicolon between them so that the shell knows they’re separate commands:
Tip: If any of your pathnames have spaces in them, you should enclose the variable definition in quotation marks, just to be safe:
PATH="$PATH:/putting/spaces in pathnames:/makes/life very/inconvenient";export PATH
Setting the PATH variable for every new shell session
The methods we’ve used so far will only set the environment variable for your current shell session; when you logout or close the terminal window, your changes will be forgotten. If you want to set PATH to a certain value every time you log in or start a new shell session, you should add it to your bash startup script. Every time you start an interactive shell session, bash reads the following files in order (if they exist), and executes the commands inside of them:
The first file, /etc/profile, is the default startup script for every user on the system. One or more of the other three are located in the home directory of every user. Any of those three can be used, but it’s important to know that they will be searched for in this order.
You can edit these files and manually change any lines containing PATH= definitions. Just be careful if you do so, because these are the directories used to locate important operating system files.
If all you want to do is add a path for your current user, you can leave the other PATH= lines untouched, and just add a line like this to the end of the file:
If you add this to the end of the .bash_profile file in your home directory, it will take effect every time your user starts a new shell session. If you add this to /etc/profile, it will take effect for every user on the system. Note that you will need administrator privileges if you want to edit /etc/profile, so you will have to use sudo (or be logged in as root) to do so.