Managing PATH on Mac and Linux
00:00
When modifying the value of PATH, you probably want to start with just looking at what’s in PATH already. So to do that, you can go to your terminal, this one is my Mac, and echo $PATH.
00:17
echo is the print statement, and the dollar sign indicates that you want to print an environment variable called PATH.
00:25
That prints it all out in one big string with the paths separated by colons. To make it easier to read, you can do echo $PATH, then pipe tr, then inside of quotes,
00:41 colon, and inside of other quotes, the newline character. That’s going to translate the colons to newline characters and show the different paths on new lines. So looking at what I have in my path, at the top there’s this one to a bad Python here that I’m going to want to remove.
01:01
There’s 3.13 and 3.14 which were added two lessons ago. And then some other values like /usr/local/bin, /usr/bin, so other commands on my machine like the cd command or the ls command, those are going to be found in one of these other base root files.
01:22
To remove bad Python from PATH, I’m going to need to figure out where it’s being added. And to do that, you’re going to need to know about how your operating system determines what’s inside of PATH, and you’re going to need to use a command line editor to edit things.
01:40
First, there are going to be some dotfiles in your home directory where PATH could be edited. And these are your profile or login dotfiles and RC files.
01:52
The files that are actually in your home directory depend on if you’re running the bash shell or the zsh shell. If you have a fish shell or something else, unfortunately, I don’t have the time to go into all the different permutations, and most people are on bash or zsh.
02:11
I’m on zsh on my Mac, so I’ve got a .zprofile and .zshrc file. The .zshrc file is usually for more of the look and feel settings, but they can still have PATH edited there, and there’s no strict rule about what goes where.
02:33
So cd ~ to go back to the home directory if you weren’t already there,
02:39
and then ls -a to see all of the files and folders, and .zprofile and .zshrc are located here. To see the contents of it, you can say cat .zprofile, and that’s the one that we looked at two lessons ago where Python 3.13 and 3.14 were being added. Now let’s do cat .zshrc, and that’s the RC file.
03:14
On the last line here, this bad Python is in fact being added to PATH, so we’ll want to remove that. Another thing to note is that I have an alias here. If you really did want to use the python command, you can alias it so that it points to some other command, in this case python3.14 with the -q option.
03:38
What that does is when you run the Python console, it doesn’t print out all of the startup info. To open up this file in a command line editor, you’re going to need to choose one, and nano is one that comes built in with Linux and Mac, so you can use nano .zshrc, give it the file name, and that’ll open it up.
04:05
On the bottom, there’s a bunch of commands that you can use if you don’t know nano already, so I’m going to use the Control+K to cut this line that exports the bad Python PATH.
04:20 and then Control+X to exit out. Note that there’s also Control+G to see the help manual to find out more commands.
04:30
Control+X, if you did edit the file, it’ll ask you if you want to save, so Y to save, and then Enter to save it to the existing file name. Now normally we would source .zshrc to run the file, but in this case we removed an item, so it’s going to still be in there in the PATH.
04:52
So to kind of refresh everything, open up a new tab or a new window, and then echo $PATH, and now the bad Python should be gone. So that’s one of the locations, it could be in your profile or RC file in your home directory, but where do these other items get added?
05:14
Those are actually inside of the /etc folder, so you can cd, change directory into /etc, so from the root, which is the initial slash, then the etc folder.
05:27
And there’s a bunch of items in here, including zprofile, zshrc, and paths, paths.d. So the ones in your home folder are going to be specific to your user, and if there are multiple users on your machine, each one is going to have its own home folder files that runs first.
05:53
But the /etc ones are going to be system-wide, so inside there, there are some shell startup files like profile and RC files, which are going to be system-wide, so you can edit PATH in there.
06:07
On Mac, there’s also the paths and paths.d, so paths, we can look at the contents,
06:16
cat paths, and it’s a file that contains one line per path to add to the initial PATH value. paths.d is a directory, and anything inside of the directory, those files can have items that get added to PATH as well.
06:36
On Linux, there are environment and login.defs files, as well as an environment.d folder.
06:45
So cd in Linux /etc, let’s look at what’s in there, there’s a bunch of stuff,
06:54
cat environment, and you can see that that has PATH equals, and then the initial value of PATH.
07:03 So in general, as a good rule of thumb, you shouldn’t be modifying any of these files unless you know for sure what you’re doing and that you won’t be breaking anything, but it’s good to know where these other paths get created.
07:18 And that’s a wrap for the course. In the next lesson, you’ll review what was covered and get some resources for future learning.
Become a Member to join the conversation.
