In Linux (also UNIX) $PATH is environment variable, used to tell the shell where to look for executable files. $PATH variable provides great flexibility and security to the Linux systems and it is definitely safe to say that it is one of the most important environment variables.
Don’t Miss: How to Set and Unset Local, User and System Wide Environment Variables
Programs/scripts that are located within the $PATH’s directory, can be executed directly in your shell, without specifying the full path to them. In this tutorial you are going to learn how to set $PATH variable globally and locally.
First, let’s see your current $PATH’s value. Open a terminal and issue the following command:
$ echo $PATH
The result should be something like this:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
The result shows a list of directories separated by colons. You can easily add more directories by editing your user’s shell profile file.
In different shells this can be:
- Bash shell -> ~/.bash_profile, ~/.bashrc or profile
- Korn Shell -> ~/.kshrc or .profile
- Z shell -> ~/.zshrc or .zprofile
Please note that depending on how you are logging to the system in question, different file might be read. Here is what the bash manual says, keep in mind that the files are similar for other shells:
/bin/bash The bash executable /etc/profile The systemwide initialization file, executed for login shells ~/.bash_profile The personal initialization file, executed for login shells ~/.bashrc The individual per-interactive-shell startup file ~/.bash_logout The individual login shell cleanup file, executed when a login shell exits ~/.inputrc Individual readline initialization file|
Considering the above, you can add more directories to the $PATH variable by adding the following line to the corresponding file that you will be using:
$ export PATH=$PATH:/path/to/newdir
Of course in the above example, you should change “/path/to/newdir” with the exact path that you wish to set. Once you have modified your .*rc or .*_profile file you will need to call it again using the “source” command.
For example in bash you can do this:
$ source ~/.bashrc
Below, you can see an example of mine $PATH environment on a local computer:
marin@[TecMint]:[/home/marin] $ echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/marin/bin
This is actually a good practice to create a local “bin” folder for users where they can place their executable files. Each user will have its separate folder to store his contents. This is also a good measure to keep your system secured.
If you have any questions or difficulties setting your $PATH environment variable, please do not hesitate to submit your questions in the comment section below.
Frustrating, because there’s very incomplete information here. “In this tutorial you are going to learn how to set $PATH variable globally and locally.” Really? I’m going to set the path for the whole world? What do you -really- mean by globally? (For all users, I’m assuming)
“First, let’s see your current $PATH’s value. Open a terminal and issue the following command: echo $PATH”
Why can’t I just type “$PATH“? Seems to do the same thing.
I don’t see anything about path in my .bashrc file.
“$ export PATH=$PATH:/path/to/newdir”
What if I make an unnoticed typo; how do I fix that?
Whenever I open the terminal in Linux, I get a new blank page, but to continue from where I had stopped, what should I do?
Hi Marin,
This works. But I need this to work automatically every-time my machine boots up. Now I have to run the source ~/.profile every time I restart. Is there any way to do this automatically?
I have vxargs.py script in directory “/home/csgrads/kumar392/RESILIO“. However when I try to add the directory “/home/csgrads/kumar392/RESILIO” to the path it gives an error – Bad : modifier in $ (/).
I used the command mentioned by you, i.e. export PATH=$PATH:/home/csgrads/kumar392/RESILIO.
Can you please help.
what about the process that running in crontab, which files should I add “export PATH” ??
Great post!
You can also use /etc/environment to set PATH environment variable, but it does not support variable expansion.
Thank you “tomatreides” for this comment. It just solved a problem I have had more more than one week.
We had this odd problem on a CentOS 7.3.1611 box where we installed Java 1.7.0_80, not upgraded or updated Java but a fresh install. What we noticed was that the PATH seems to have been ruined by something, but we could not figure out by what or how. You just solved that!
Our theory is that the Java installation wrongfully puts its stuff in the “/etc/environment”-file and that then completely ruins the PATH variable since the PATH then contain “$PATH” and “$JAVA_HOME” instead of its expanded content.
The fix is to move the Java related content from the “environment” file into a new file in the folder: “/etc/profile.d/”. Using the files “/etc/profile” or “/etc/bashrc” or any other system wide file is not recommended. Using e.g. “~/.bash_profile” or “~/.bashrc” or any other user specific file is also not recommended. The files in “/etc/profile.d/” are also not affected if you update the OS. So this is imho where this type of things should have been done in the first place (Oracle, are you listening?).
Maybe there is a better way to solve this, but this works for us. I hope this helps anyone else who have the same problem. If you want to know more please reply to this comment and I will be more than happy to help you.
Nice article .. If we need to use java on ubuntu then what are the parameters we should use? or on which file.
Any help will be appreciated!!
thanks its a very informative ..
Great post and great blog!!!
In my PATH I have some variables repeated, e.g., /usr/local/bin
I’ve never had issues with that, so I think that it should not be so important to have duplicates, am I wrong?
The path is resolved with the first match. If you have identically named executables then the specific order of the paths are important and you might find unexpected behavior when there are duplicate or out of sequence entries.
you should include fishshell