Check out a free preview of the full Introduction to Bash, VIM & Regex course
The "$PATH" Lesson is part of the full, Introduction to Bash, VIM & Regex course featured in this preview video. Here's what you'd learn in this lesson:
James introduces the special environment variable $PATH
Transcript from the "$PATH" Lesson
[00:00:00]
>> James: Now we can get a bit more into like how your system works, and a few different environment variables that it's really good to know about. So, this whole time we've been doing things like learning commands, but it's important to know kind of where these commands even live.
[00:00:17]
These are all individual programs, but where are they? So If you do echo $PATH, all caps, you can see.
>> James: So $PATH is a list of colon separated file paths. So the default ones are places like usr/local/bin, and user/bin, and /bin and /sbin, and that kinda stuff. Different systems are gonna be configured different ways, but there are a few places where you can reliably expect they'll be in the path.
[00:00:52]
If you want to make your own systems, if you were gonna make your own scripts that were available system-wide, probably the best place to put them would be in usr/local/bin. I'll check and show you what mine looks like. So if you ls usr/local/bin, here, I've already got a lot of little scripts in there.
[00:01:11]
The operating system generally doesn't touch /usr/local/bin. So it's safe to put your own stuff in there, and you don't have to worry about your package manager or something, going in and moving things around, because it will generally use places like user/bin or /bin to do that. So here, I've got some files, all of these files now.
[00:01:35]
Any of these programs, so long as they have the executable bit set, which you can do a chmod +x. And we'll talk more about permissions in a little bit. Any one of these is now available in my path, so I don't have to type usr/local/bin/ipfs, for example, to use the ipfs program.
[00:01:52]
I can just type ipfs. This is also true for our logger example, if I just go back in and put back the original one.
>> James: If I wanted to install this system-wide, what I can do now it's sudo, because /usr/local/bin/ is a system-wide path. So I can copy my logger into /usr/local/bin/, and I can give it a name, and whatever name I give it will be the command name that it's gonna show up as in my terminal.
[00:02:26]
So I could call it loggy maybe for, I don't know. Now, I have a command. A new command called loggy and,
>> James: I can see my message in the script.
>> James: So it's quite common to, like I showed a bit with the MPM config, to have things like your path set up in your bashrc file.
[00:02:55]
So I can just share what mine is like. So here's my path. Each of the pieces which is split by a colon are the places where Bashful looks. So it starts at the first one, then it goes to the next, and the next, and the next. And the first one that it finds that has the same name whenever you type a command into the command line, it'll use.
[00:03:16]
So, it first searches my own prefix path which is in my home directory, right here. And if it doesn't find anything there, then it goes to the other locations.
>> James: Right, so. bashrc file, if you haven't noticed is also a shell script. So you can put whatever commands that you want into here, and they'll be run every time you open a new terminal.
[00:03:48]
For example, a fun thing to test is if you go to the end of your bashrc, or anywhere really in it, and you put a message like echo HELLO WORLD. Now, the next time you open a new terminal, I see the message HELLO WORLD. Now, anytime that I log in or open a new terminal, I'm gonna have that command run.
[00:04:11]
So, you can put whatever you want inside of there, whatever kinds of stuff, whatever kinds of environment variables, whatever kinds of set up. You could print a cool comic. There's a program you can get called cowsa. I don't have it on here, but it just says a message with a cow.
[00:04:32]
It's pretty cool. I recommend you install it. There we go, there's a cow. You could put that in your bashrc, whatever you want.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops