The $PATH variable tells your terminal where to find the tools you have installed. The directories list in your $PATH will be used to determine which tool to use when you run commands in the terminal. Since developers often use different versions of the same...
Duration
4 min
Access
Pro
Transcript
Available
The $PATH variable tells your terminal where to find the tools you have installed. The directories list in your $PATH will be used to determine which tool to use when you run commands in the terminal. Since developers often use different versions of the same tool, it's very important to know exactly what your $PATH is doing.
This lesson walks through how the $PATH variable works and how updating the $PATH affects your development workflow.
Video Transcript
Instructor: Developers have many tools installed on their computers such as Ruby. We'll check the version, may be Code. We'll check the version. Node, check the version, but how does your terminal know that Ruby and Code and Node all exist? The terminal gets that information from the PATH.
If we echo the PATH variable, you'll see a colon-delimited list of directories. To format this better, we'll echo PATH and use a command to replace the colons with new lines. This is the same thing, just with new lines.
You can see all these directories where it would find those executables. If we look for Code, you'll see Code is located here, which means that this directory must be on our PATH. If we look through these directories on our PATH, you'll see it right there.
Any of these tools you have installed all look for Kit. You'll see this directory, which must be on our PATH. You'll see it right there. PATH is a variable with a bunch of directories. We can set PATH to any directory we want. We'll set it to this. Now, our terminal can find Code, but our terminal cannot find Kit, because if we echo our PATH again, you'll see even this tool isn't available, so we'll echo PATH here.
You'll see the only thing on our PATH is this. Each time you open a new terminal window or a new tab, you'll see that our PATH is restored because any changes you make to the PATH like this only live in this session.
Each time you open a new tab or a new window, the terminal does a login sequence which loads some files, which create the path. I'm using Zsh, so one of the files Zsh or Z shell loads is called the .zshrc. Bash would load a .bashrc and so on.
You'll see these statements like this where you assign the PATH to the previous version of the PATH, plus a new directory. There's the colon, which separates those directories. If you look at our list of PATHs, you'll see these three directories right here, which line up perfectly with these three directories.
If you were to forget to add the previous version of a PATH like that and then open a new tab, you'll see it will probably complain of things it's trying to do during setup because those aren't found anymore. If I echo my PATH now, the only thing on it is this.
Be extra careful that any time you export your PATH, so that is assigned every single time you open a new terminal, that you add the previous version of the PATH.
This also works the other way. If, say, for example, you wanted this version of Ruby to load, even if there's already a version of Ruby on the PATH, then you would put it in front of the PATH. This directory would take precedence over anything that's already on the PATH.
This could be important if you have multiple versions of the same tool installed and you want to make sure you're using the exact tool that you need. Say, for example, I have Node and it's this version. That Node on my path is located here, but I want to use this version of Node in this directory.
I could set my path to be equal to this directory, colon, and the previous version of the PATH. Now, when I run Node version, you'll see we're getting this version because this PATH is taking precedence over anything that's already set. If I echo out my PATH, you'll see that the first thing in the list is this directory.
Since I just did that here, that's just a temporary change which will go away. If I open a new tab, a new window, now I'm back to the Node that's on my PATH.
Access required
This lesson is available with an active egghead membership.