Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In the last movie, we saw how commands and programs are really just files that are being executed, and we saw that we type echo 'Hello world' that what it's actually doing is executing a file that is located in bin/echo. I write the argument. Those are doing exact same thing. This is just a shortcut for finding this file and executing it. So how does Unix do that? How does Unix know to find echo in this location or to put it another way, if I create another file named echo, how does Unix know not to use that one instead, or what if I wanted it to use that one instead? How would I go about doing that? Well, the way that Unix manages all of this is with a variable called PATH.
$PATH will show you the current value of your path and the path is always a colon-separated list of file paths and this is the list that Unix will use when trying to locate commands to execute. The early entries take precedence over the later ones. So what it does is it starts at the left. When we type echo, it says, "Oh! I'll look in user bin. Do I find echo there? No. All right, I'll look in bin. Do I find echo there?" Well, in this case, it does and so it runs it. If it didn't though, then it would look in usr/sbin, sbin, usr/local/bin, and finally user/X11/bin.
If it didn't find it in any of those places, then it would say "Oh, the command is not found." You can see that behavior. If you just type some junk on the screen and hit Return, it comes up and says command not found. What it actually did was checked all of those directories in order and then said, "Nope, it wasn't there. It must not be a command that's available to me." Now, you can change this path in bash. We do that with just typing PATH, no dollar sign in front of it, equals, and then the path that we want it to be, Different shells choose a slightly different technique for setting the path. I am just going to show you the bash way.
Let's just grab this string here and I'll use Command+C and then Command+V to paste it and that will give me everything except that very last path. So I am just leaving off that last path and the colon that's before it. Now, if we go up and do echo $PATH, you can see my path has changed and I've omitted that last directory. What that means is that any commands or programs that were in that last directory, Unix will no longer look there. It won't be able to find them. I have essentially taken them out or uninstalled them. We can do the same thing by adding our own directories. If we have other places we want it to check, we can add that in there.
Frequently, when we do web development, we want to add a MySQL path so it'll look inside our MySQL folder to find all those MySQL commands. That's a very common use of this. Let me show you an extreme example. If we set our path equal to just simply usr/ local/bin and let's echo our path, that's it! It's just going to check this one place. We could use echo, we could try that as an example, but echo is such a fundamental and that it's actually really built into the system. The system will find it anyway. As a different example, let me use ls.
That's another pretty fundamental one but if we try ls now, it says oops. Command not found. It didn't find the ls command anymore. Now, you could change your path back and just paste that back in, or I want to show you that if you close the window and you reopen it now, now if we say ls, we get our directory listing. That's because setting the path in the way that we did only lasts for the current session. It doesn't stick around. In Chapter 7, we are going to talk about configuring your environment and you'll be able to set a path there that will stick. So if you want to know how to do that, Chapter 7 will cover how to make a path last.
But I want to make sure that I show you here is the concept of the path so that you will understand how it's translating these commands into executing the files that may be located in different locations. There is a last thing. Let me also just mention that the command that we learned in the last movie, which is the which command, so if we say which ls, it tells us which version of ls we are using. What that means is there may be several versions here, but I'm using your PATH variable and I am telling you which one I'm going to give you. So that's important. which is slightly different than whereis because which uses our path to say this is the one that's active.
So it's a really good command to know. So for example, maybe we have several different versions of Ruby installed. Well then we could say which ruby, and it will come back and tell us the version of Ruby that we will get if we were to type a command that started with Ruby. Okay, now that we have those fundamental concepts of how commands and programs work, we are ready to actually start diving in and learning some useful commands.
Get unlimited access to all courses for just $25/month.Become a member
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.