What is $PATH on a Linux Shell? (The Linux Crash Course Series)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
thank you hello and welcome back to learn Linux TV and also welcome back to the Linux crash course Series in this series we go over one very important topic around Linux one video at a time and unless I mention otherwise each video in the series is completely Standalone in today's video what we're going to do is talk about path and no I'm not talking about the path to Enlightenment although if you know about the path to Enlightenment and you have some tips definitely let me know but no it's not about that there's actually a path variable in your Linux shell and that path variable actually includes a listing of directories that are important directories that include binaries or scripts that you can run and that's actually what makes or breaks your ability to run a command by default from a particular directory so we're going to take a look at that now before we do I just need to take a moment to mention the sponsor for today's video which is lenode if you haven't already heard of lenode well they're a Linux Focus Cloud Server provider and with their platform you can spin up your very own Linux server in mere minutes and considering that there's over 40 videos in this series as of recording time with many more coming the note is a great service that you could use for spitting up test servers and you could use those test servers to go along with the various tutorials that are available on this channel and by using the URL that you see on the screen right now that'll get you one hundred dollars in free credit towards your new account and that credit is good for up to 60 days and by using that URL you are actually supporting learn Linux TV and I'd greatly appreciate that and their platform is great for more than just spinning up test servers the note is the real deal when it comes to providing cloud computing resources you can literally run your entire business on the Node or you could use it to do something fun like spitting up a Minecraft server so you and your friends can build a World online together the only limit is your imagination so definitely check them out thank you so much to lenode yet again for sponsoring another episode of the Linux crash course series I really appreciate that now the concept of a path in your shell is a very important concept so this video is actually going to be one of the easier videos in this series but it's also going to be one of the more important ones so I definitely want you guys to pay very special attention to this because path is really important in fact critical to your shell and how you run programs so let's go ahead and dive in and I'll tell you all about it [Music] so here I am on my Linux workstation and I'm ready to show you guys all about path and what it represents but I think a fun way to understand just how important path is is to imagine a world where we don't actually have it so to show you just how useful path is I'd like to take you to an alternate universe where the concept of path was never invented and as part of that I'll run some commands in my terminal and you should start to see immediately that the commands that I'm going to be running are way more complicated than they need to be so let's warp into that alternate universe where path was never invented and see what that looks like foreign so what I'm going to do in this example here is I'm going to use the find command to find a file on my file system I had a notes file that I saved earlier and I just have no idea where I saved it now the first thing I should probably do is see whether or not that file is in my current working directory I'm sure you're already familiar with the ls command so I'll use the ls command to see if that file is right here in my home directory so just like any Linux administrator what I'll do is run LS and in case you didn't already know that slash user user is actually abbreviated so it's slash USR slash bin and then slash LS and I'll also use the options Dash L and dash H as well and I have two directories here but I don't have my notes file so where exactly did I save that well I don't know I think what I should do right now is use the find command to find that file that would be a good way to do it so I'll type slash USR slash bin and then slash find I want to look at my current working directory so I'll type a period here for that I want to return results only for files so I'll use Dash type and then f for files I am not interested in directories I know it's a file so I'll search for files and for the name I know that I called it notes.txt so this should actually tell me where that file is located so I'll press enter oh it's right there in my documents directory I probably should have known that maybe I'm just tired or something anyway what I want to do is add another note to that file so let's open it up in an editor so type slash user slash bin and then Nano and I'll type the path to where that particular file is located so I'll just open this up in an editor and hold on a minute this is way too complicated do I really have to type slash user slash bin in the front of every single command that just seems overly redundant and even a little bit tedious there has to be a better way [Music] well actually there is and that's what path allows us to do path allows us to shorten commands it's literally the reason why we could type Nano bind LS commands like those without typing the path to those commands we could just type the name of those commands that makes everything a lot easier and what path actually is is a variable and just like any other variable we can see the contents of the path variable by using the echo command and I'll type dollar sign path path is in all caps in this case and I'll press enter and check that out path is actually a list of directories that's separated by a colon so anytime you run any command on the Linux shell it's going to check each of these directories for a command or a script with that name and when it does that it'll look in slash user slash local bin also slash user slash bin slash bin and so on so those are the directories that the shell is going to check anytime you run a command if there's a matching command that matches the name that you've typed within one of those directories it's going to run it if not it's going to tell you that the command is not found now as a quick example of this I'm going to write a script it's going to be a very simple script I'm not going to go over bash scripting in this particular episode but I think a script would be a great idea so what I'll do is create a new command or a new script and what I'll do is I'll just name it mycmd simple enough now to start off the script I'll type the usual shebang at the top right here and I'm going to make this extremely simple so what I'll do is Echo hello world just like that that's the entire script so I'll save the file I'll exit out I'll also Mark that executable as well and as you can see we have my CMD right there now for fun what I'm going to do is move my CMD into the documents directory and let's go ahead and execute the command again I called it mycmd so let's run it and I get command not found now that actually makes sense I moved it into my documents directory so I'm literally referring to a script that isn't in my local working directory here that's why I moved it into the documents directory just to make sure that it's out of the way and we got a command not found error message which we did so what I'll do is actually move that back to my current working directory I'm in my home directory right now I'll just move it here and there it is so now that I have that script in my current working directory let's see if we can run it now still command not found that's weird it's absolutely right there what's going on well when we checked out the contents of the path variable we saw that user local bin is one of the folders that is referenced right here so what I'm going to do is move my CMD into slash user slash local bin now because that's actually a protected directory that I don't have ownership of I will need to use root or sudo in order to do this so let's move it in there all right so let's just check the contents of that directory you can see that my CMD is inside that directory now currently I'm in my home directory but check this out I'm now able to run the command why am I able to run it now well because I moved it into a directory that's within my path and as you can see it works now similarly if I was to move that out of user local bin let's say I want to move that into the user bin directory and again I'm going to need sudo even I forget that from time to time and there's a lot of files in user bins so I'm going to grep for the name of the file that I've saved there and we can see that it's right there we're not always going to have colorized output but given that we have x's in the permission string we know that it's executable anyway even though I moved it to a different directory it still works so I moved it from user local bin to user bin and the reason why that works is because both of those directories were actually a part of my path but what's interesting here is that my home directory is not so let's go ahead and move that script back to my home directory then I'll move it right here now since my home directory is not a part of my own path that's actually why I can't do this it doesn't work no such file or directory which might be misleading to some of you because it's absolutely there but I can't run it actually I can run it what I could do is type Dot and then forward slash and then the name of the script but this is actually telling the command line to do is run my CMD which is located in my current working directory that's what the period means a single period is going to represent your current working directory so I'm telling my shell to look at my current working directory for a script or a program basically that's named my CMD and it is present in my current working directory so what's going to happen here it works so that's actually how you run something that's not within your path so it's my home directory which is where I am right now since that's not located in my path it's not going to recognize the command simply by its name but again if I wanted to run a command or a script or something like that I can type the full path to that particular command or script and that'll work just fine so typing dot slash mycmd is pretty much the same thing as typing slash home slash J in my case my CMD I'm typing the full path to that command it works just fine now if you're curious by the way where a command is located if you type which and then the name of the command in this case LS is slash user slash bin slash LS I could also run that by typing slash user slash bin slash LS just like that but it's actually a lot easier to type simply LS now as an aside you'll notice that the output is different when I type LS as compared to typing slash user slash bin LS that actually has to do with aliases that's outside the scope of this video but you can check out the aliases video I've done that I'll go over that so I won't go into any detail about that but yes I know the output is different and it's supposed to be but we're going to ignore that for now now you might be wondering why is the home directory not a part of path well actually that's a good thing we really don't want to have commands and scripts run directly from our home directory unless we explicitly call the script and somebody was to commandeer their way into our system the ability to run programs from just about everywhere would actually work against us because that would give that person privileges to run commands themselves from any directory so if they had a script that they loaded on your system or you know something else like that then that's going to be a very bad day but by limiting where commands could be run from that actually increases security most of the directories in our path which in the case of Debian which is what I'm using right here these directories are part of the default path so scripts and programs can be run from any one of these directories but my home directory is not there which is why simply typing my CMD without the period and slash in front of it that didn't work but how do you go about actually adding something to your path well let's take a look at that and what I want to do in particular is actually enable the ability to run commands from my home directory but not the root of my home directory what I want to do instead is actually create a directory in my home directory called bin and this is very common you'll probably see this a lot in the real world an experienced Linux administrator might actually create a directory named bin in their home directory and inside that directory would be a bunch of scripts and helpful programs and things like that again that's very common it's not required it's just something that generally happens in the real world and actually what I'll do is move my script into that bin directory foreign which is where it is now however that still doesn't fix our problem that the script isn't recognized no such file or directory now for some of you creating the bin directory and moving the script into that directory that might actually work just fine some distributions actually include the bin directory in your home directory as part of your path now in my case in the case of Debian it's not by default added to path so the bin directory in my home directory is not recognized as a directory from which I can run commands so what I'll do is add the bin directory that I've just created to my path right now let's see what that looks like so what I'll do is I'll create a new variable named path and when we create variables if you didn't already know we don't need the dollar sign we only need the dollar sign when we refer to a variable that's already been created but when we create a variable we'll leave the dollar sign out of it anyway the new variable is going to be called path and I'll set that equal to something but before I do I just want to mention we already have a variable named path so by creating a new one with the same name that's going to override the one that exists however what I don't want to do is lose the directories that are currently in my path I want to retain those but also add a new directory I don't want this new directory that I'm about to add to replace all the other ones that would make the shell act all kinds of ways that are weird so we definitely don't want that but a quick and easy way to solve this problem is we could actually set the New Path variable to be equal to the current path variable again when we create a variable we don't need the dollar symbol but we do need it when we refer to an existing variable so in this case the new variable is going to be exactly the same as the path variable that already exists however what I'm going to do before I press enter is add another colon tilde and then slash and then bend just like that so putting this all together I'm going to grab everything that's already included in the path variable the one that already exists and I'm going to add the bin directory in my home directory to that existing path and the New Path variable is going to be equal to the old one plus the new directory that I'm adding and what you could also do is add another directory another one another one and so on you could add as many as you would like to add but for me I think this is actually fine so I'll press enter and if I Echo the contents of the path variable here we can see Slash home slash J bin is there at the end so I was successfully able to add a new directory to my path which also means by CMD by itself is going to work and when I type which my CMD you can see that it's located in my bin directory just like it's supposed to be so by adding the bin directory that I've just created in my home directory to my path then any script or program that I save within that directory is a programmer script that I could run from the command line regardless of where I happen to be for example I can go all the way into the Etsy directory it doesn't really matter and the command still works because the shell is now going to check my bin directory for scripts and commands and you know what the bin directory would not be a bad directory to create on your end but also be a good thing to add to your path as well but one issue with this is that when you close the terminal then the path variable is going to return to normal the next time you open a terminal it's just going to be back to the way it was without the bin directory so if you want to make that stick here's how you do that we can bring up a text editor and there's a special file that we can edit in our home directory in the case of bash it's stop bashrc that's the configuration file for bash itself and depending on your distribution it'll probably look a lot different every distribution maintains their own bash RC file but anyway this file is read every single time we open a shell so basically what I could do is add that same command that I just used to add bin to my path I can add that right here and again the terminal is going to read this file every time it opens and it's going to execute whatever code I put in here to add it to my path now what I'm going to do is take this a step further though I'm going to write an if statement and you don't have to worry too much about what I'm doing as far as scripting is concerned I have dedicated videos that are all about scripting actually but anyway what I could do is test for the existence of a directory and what I want to do is check and see if the bin directory exists in my home directory so then I'll add a closing bracket right here and what this is going to do so far is it's going to execute the statement that I'm about to type only if the bin directory actually exists so again I'm testing for the existence of a directory and if it's found I want it to do something and more specifically I'm going to have it run the exact same thing and then I'll type if backwards to close the if statement now the reason why I'm doing it like this is because if for some reason your bin directory doesn't exist then this is just going to error out every single time you open a terminal but by adding an if statement then what's going to happen is the shell is going to test for the existence of this directory first and if it's found it's going to add it to the path if it's not found well then the if statement proves false and nothing gets executed at all so if you decide in the future that you don't want to have a bin directory anymore that you don't have to worry about removing this and then later on if you decide to have a bin directory again then it's going to be recognized anyway I'll save this file Ctrl o and then enter and then control X and that should do it now with every terminal that I open I'll have that folder as part of my path and as a result of that It'll recognize commands that I might have saved inside that folder now if nothing else you should Now understand how path plays a role in how you execute commands on the Linux command line it's an important concept but thankfully it's really not all that difficult it's one of those that might seem difficult when you're just starting out but hopefully this video made it easy for you foreign was helpful in teaching you guys all about path on your Linux shell it's a very important concept something that I recommend you pay special attention to it'll definitely come in handy I can guarantee it anyway thank you so much for checking out this episode of the Linux crash course series I really appreciate it there's going to be additional episodes coming very soon and I can't wait for you guys to see these episodes so definitely subscribe so you'll be the first to see these as soon as I have them uploaded but in the meantime thank you so much for watching I really appreciate it and I'll see you again very soon [Music] thank you [Music]
Info
Channel: Learn Linux TV
Views: 17,487
Rating: undefined out of 5
Keywords: Linux, LearnLinuxTV, Learn Linux TV, LearnLinux.TV, Learn Linux, Linux Training, Linux Tutorials, $PATH, Linux $PATH, PATH in Linux, Linux for Beginners, Linux Essentials, Linux Crash Course, Linux Tutorial, Linux Tutorial for Beginners, cloud computing, linux server, Linode, VPS, Server, cloud computing course, cloud computing tutorial for beginners, linux for beginners 2022, cloud hosting, linux server commands for beginners, CLI, Commandline, Linux Commands, Linux CLI
Id: hk0RwVC6uts
Channel Id: undefined
Length: 21min 52sec (1312 seconds)
Published: Thu Nov 03 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.