What is the Unix PATH variable and how do I add to it?
Solution 1
The UNIX path is an environment variable which is a list of directories in which to look for programs that you're trying to run. It allows you to avoid having to use the complete pathname for running things like /bin/ls
(for example by putting /bin
in the path).
For example, a path may consist of:
/bin:/usr/bin:/usr/sbin
and that means, when you type in the command xyzzy
, it will try to run the first file it finds from the current list:
/bin/xyzzy
/usr/bin/xyzzy
/usr/sbin/xyzzy
(it may skip non-executable files if it's being clever).
You can add things to the path with a command like:
set PATH=/directory/to/add:$PATH:/low/priority/path
which places /directory/to/add
at the start of the path search list, and /low/priority/path
at the end.
However, this usually only changes for the current shell. If you want to make a change in every shell, you should add that line to one of your startup files, like $HOME/.profile
or /etc/profile
. The correct file to use depends on your shell itself and how you've set up the startup files. It's not always easy to tell where it should go but the rules are generally explained in the manpage for whatever shell you're using.
You can usually find a command in the path with one of:
which cmd
whence cmd
to locate the cmd
executable. For example, on my Debian system, I get the following transcript:
pax> which ls
/bin/ls
pax> which firefox
/usr/bin/firefox
pax> which xyzzy
pax>
Solution 2
What is?
The PATH environment variable is a colon-delimited list of directories that your shell searches through when you enter a command.
How to set it?
PATH=$PATH:/your/directory
export PATH
How can I make it stay on the PATH every time I open the Terminal?
Put the previous two lines inside ~/.bash_profile
(if you are using bash
for the Terminal).
Solution 3
PATH is a environment variable for unix like systems.
set path:
export PATH=$PATH:<your path>
unset path:
unset $PATH
set path permanently
in your home folder, enable View --> Show Hidden Files.... pen .bash_profile file, before export PATH line, add this line.....
PATH=$PATH:<yourpath>
logout and login again...... check if its working ... well ! it should work.....
Related videos on Youtube
Kevin Burke
I build reliable software and design great experiences. I'm available for hire: https://burke.services
Updated on September 18, 2022Comments
-
Kevin Burke over 1 year
Many programming tutorials ask users to add something to their
PATH
but don't explain what it is. The existing resources that explain thePATH
to users new to the command line are not very good.What is the Unix
PATH
variable?I just downloaded a program, then tried to run a command in the terminal and got
command not found
. What does this have to do with thePATH
?I added something to the
PATH
with this command and then things worked:export PATH=/path/to/some/bin:$PATH
...but then I got
command not found
again the next time I started my computer. How can I make it stay on thePATH
every time I open the Terminal?What's the best way to determine if a program like Git or
gcc
is already loaded on my computer?
-
trojanfoe over 11 yearsI bet I can find 50 hits if I search google for this question. Why can't you?
-
Kevin Burke over 11 yearsI've read through many of the answers on SO and I haven't found a answer that answers every one of these questions in a way a newbie can understand. I want something I can link new command line users to. It was my hope to add this as community wiki and attach significant bounty to it.
-
Admin over 11 yearsAnd if none of those links points to SO for a question that's qualified to be here, then it should be added here. We want SO to be where Google goes for programming questions.
-
jmort253 over 11 yearsPlease see this MSO post for Kevin's motivations in asking this question. He'll most likely make an edit to clarify how this information is helpful.
-
Madara's Ghost over 11 years@jmort253: Well, he should start doing it quickly. 4/5 close votes. I won't close because I know wants to edit the question, but he better make it quick.
-
trojanfoe over 11 yearsDoes this question belong on stackoverflow?
-
Admin over 11 years@trojanfoe, as much as any shell-related question does, yes, I would think so. It is also a programming language.
-
Joachim Sauer over 11 yearsI modified the answer to use
:
as the separator, as that's used on *nix. Windows uses;
(probably because:
already has a meaning inC:
). -
Admin over 11 yearsActually, @Joachim, it's possibly more correct to say that a given shell (rather than UNIX itself) has a specific directory separator but you're right for all the ones I use off the top of my head. Thanks for the fix, cheers.
-
Joachim Sauer over 11 yearsSorry for the downvote, but this is poorly formatted and phrased. Try to avoid excessive use of bold, excessive use of ellipsis ("...") and write full sentences. That should help avoid such downvotes in the future.
-
Joachim Sauer over 11 years@paxdiablo: yes, I thought about that but I too can't think of any unix shell that doesn't use
:
(in Java, for examplepath.separator
is always:
on *nix). -
Yeti over 11 years@Joachim Sauer why dont you edit the answer? Why do you prefer to downvote it?
-
Joachim Sauer over 11 years@MikroDel: because there are already better, higher-quality answers to this question and downvoting takes less effort. If this where the only answer, I would probably have edited it. Also: although they are usually no fun, downvotes are a legitimate tools (yes, there are up and down arrows next to each question/answer).
-
Yeti over 11 yearsedit or post this comment take near the same time, but edit make it better, and help Shantanu Banerjee and other users
-
Joachim Sauer over 11 years@MikroDel: so explaining the reasoning behind a downvote doesn't help the user? I think I've been pretty clear on my reasoning and have given practical tips on improving the quality. The only thing your insistence does it reduce my willingness to explain my downvotes (and as you can see in my profile I don't downvote excessively).
-
Yeti over 11 yearsIm not meaning you to downvote excessively. Its only a point of view - downvote or edit
-
Yeti over 11 yearsYou have commented you downvote it "+" I mean. Many user downvote something without any explanation. So my preference 1. Edit, 2. Downvote with explanation 3. Flag )
-
trojanfoe over 11 yearsMost of the time the shell is used interactively, and not to run scripts, and therefore non-developers have an interest in setting
$PATH
. The examples cited in the question are all interactive. Therefore I think this question belongs on a non-developer site. -
user1301428 over 11 yearsWhy is there a $ before the second PATH in
set PATH=/directory/to/add:$PATH:/low/priority/path
? -
user1301428 over 11 yearsAlso, couldn't the whole string
$PATH:
be deleted since the separator is the colon? -
William Pursell over 11 yearsIn
csh
, you can runset PATH=/directory/to/add:$PATH:/low/priority/path
. However, in thesh
family (bash, ksh, zsh, etc), that does not set the variable PATH at all, but assigns to$1
.