Transcript from the "Anatomy of the Command Line Interface" Lesson
>> So hopefully all of you got multipass running.
>> Again, multipass will be kind of the easiest way cuz that's how I wrote the course was with multipass. But again WSL would work fine, VirtualBox, VMware Fusion, Parallels, all. Any sort of virtualization software works great, or just installing Linux directly would work great, too.
[00:00:22] I'm gonna call it that we are using Ubuntu 18, 18.04 which is Bionic, I believe, the name Bionic, I don't remember what the animal is, but anyway, it's Bionic. As of today, 20.04 is out and I think you could use 20.04, but if you see any oddities and you're using 20.04, just maybe suspect that something changed in the Ubuntu version.
[00:00:48] So again, we're using 18 for this course. Okay. So, again, this course assumes no necessarily any prior experience with using a command line. So if you have some experience using command line, just bear with me a little bit. We'll get through it pretty quickly. But I just want to make sure that everyone starts in this kind of base level page of what's it like to actually interact with the command line.
[00:01:13] So, bash is actually a programming language that you run interactively. So you can actually program entire programs using just bash, or bash scripts, or shell scripts or whatever you want to call it. And when you're dropped into an interactive shell, that's what's called a REPL, R-E-P-L, which stands for read, evaluate, print, and loop.
[00:01:34] And it's an interactive way of programming. When you're in a shell, you're actually just programming one line at a time waiting for the results, right? So that's what a REPL is. And you can actually run node and Python as a REPL as well, right? So if I open my shell here, And I run something, well if I run python3, which is installed here, you can see I'm actually in a different REPL now for Python, so that's what this is.
[00:02:03] You can see here that I have a programming prompt that I'm able to program one single line at a time. Cool, so Linux is very file system oriented, right? So that's what everything is, basically everything is a file in Linux. Suze Hinton asked me to say that on air so that's why I'm saying that.
[00:02:30] So thanks, Suze. [COUGH] So yes, everything is a file on Linux. And so it's kind of like Finder on your Mac or Explorer on your Windows machine, where you're always somewhere in the file system at any given time. So if I write pwd, you can see right now I am in /home/ubuntu, so I'm inside of the home directory.
[00:02:55] And inside of that home directory, there's another directory called ubuntu. And you'll notice that I use the term directory and folder a lot, and I'm using them totally interchangeably. They might have different meanings, but I don't know what they are. So I use them totally interchangeably. So if I say directory I mean folder, and if I say folder I mean directory.
[00:03:17] Okay, so that's what this is. I'm always somewhere inside of the file system at any given time, and you can always see where you are by typing pwd. Which stands for present working directory, I think. Okay, so. And let's talk for a second about shells versus emulators. So right now I have the Terminal up app, right?
[00:03:43] And the Terminal app inside of Mac is called an emulator. That's actually what's running bash inside of it. Your emulator can run any number of shells. In fact, if I just open another tab here right now, you'll see here this actually dropped me into Mac. So, This tab right now is running bash inside of Linux, and this tab is running zsh inside of macOS.
[00:04:10] So that's why an emulator is different from a shell, because the emulator contains the shells that are running. And there are multiple different sorts of emulators that you can use. I'm just using the Terminal one directly, the default one for Mac because it's actually pretty good, it's pretty fast.
[00:04:26] A lot of people use iterm, which is another really great one. For the longest time, that's what I used. Until Terminal.app kind of improved a lot with Catalina. If you're on Windows, there's a bunch of different choices as well. The one that I obviously I'm a little biased towards is Windows Terminal.
[00:04:43] It's a new one that Microsoft is putting out. I think it's pretty good. There's also Hyper and a bunch of other stuff as well. So that's your emulator. And then inside of your emulator, you're gonna have a shell running. So your shell is gonna be bash in this particular case, and everyone should be running bash right now.
[00:05:05] I will say that if you're running zsh, you're probably okay to run zsh because they are so similar. But pretty much everything else is gonna be too different for this course to work with. Okay, so your shell is bash. Bash stands for Bourne-Again Shell, which is meant to be a kind of a funny pun, which computer scientists love mildly funny puns.
[00:05:27] The previous shell before bash was called Bourne shell. And so to make fun of it, they made the Bourne-Again Shell. And Bourne was the name of the last guy that invented the Bourne Shell. And then it just stuck forever because that was the better shell. So bash is about 30ish years old.
[00:05:47] It's great, people still really like it, it belongs to the GNU Foundation. So it's free and open source, it will forever be free and open source. It's actually has copyleft on it. So any improvements you make on bash have to be open sourced, which is why macOS uses zsh.
[00:06:12] There are other shells. And again, what I'm going to teach you today mostly applies to them. So if you're talking about ash or zsh, or cmd.exe, or PowerShell, a lot of the stuff will apply to it but they'll just be slightly different. I'll be honest with you, actually, my favorite shell is fish shell.
[00:06:30] But it's different enough that you have to do some different things. Going back to our shell. You're always somewhere inside of, when you have a session running, right, so I have one session right here, it's running, and you're always somewhere. And again, you can type pwd, which is a command in Linux that will tell you where you are.
[00:06:55] So that's the first thing to know when you're running bash commands. Is the first thing you put in there is going to be what command you're actually running. So we're just running pwd with nothing else fed into it, which is going to give you where you currently are.
[00:07:12] Okay, actually, let me fix my notes here. Okay, and then you have folders that are nested within folders. And there's actually a root directory here which is called root, right, and that's just a slash. So you would, you would represent that as just a slash. And by running pwd inside of your shell here, you've run your first basically program, right?
[00:07:54] Your first bash command there. One thing I'm gonna throw out here just really early in the course is, every command within the command line works differently. It does different things, it has different options. And I could never keep them all straight because there are so many of them.
[00:08:12] So if I do --help on the end of basically anything, it'll tell you how to use it. Now in this particular case, this might not be that useful for pwd, but I just want you to know up front. If you're ever getting confused of how something works or how you're supposed to use it, you can always just tack on a --help, and it will tell you, here's how you use this particular command.
[00:08:38] Cool. And then just know that every time that I type clear, all I'm doing is just clearing all the stuff off the screen so I can keep writing. But if you scroll up here, you can see that it still retains all of it. Okay, so now I'm inside of /home/ubuntu, which is the home directory of the Ubuntu user, right?
[00:09:01] You can see right now I'm logged in as ubuntu. If I wanna go to the home directory, I'm gonna say cd /home. And you'll see now if I type pwd, I'm no longer in the /home/ubuntu directory. I'm inside of the home directory, right? So imagine if you're in Finder or if you're inside of your file explorer, this is like clicking up one directory.
[00:09:28] So now I'm in the home directory. And if I wanted to go back to the Ubuntu directory, I could say cd /home/ubuntu like that. So cd stands for change directory, and this allows you to navigate around your computer. You'll find it's probably the most used command that you're gonna have.
[00:09:47] Now, I know some of you are sitting there thinking, wait, I can do this faster like I don't have to type out the whole qualified path or the absolute path every single time. That's true. So let's say if I just wanted to go up one directory instead of just being where I am right now, I can type cd .., and dot-dot represents go up one, right.
[00:10:11] So if I do that, and I do pwd again, you'll notice I'm in home. And if I do it again, you'll notice that I'm in the root directory. Right, or I can go into cd /home/ubuntu. And if I want to go up all the way to the root directory, you can actually do CD ../..
[00:10:35] And you'll notice now that I'm in the slash directory as well. So that's what that dot dot means. It's called a relative path, right, where I'm telling you to go up one directory. Any questions about that? Make sense so far? I imagine many of you have seen this before.
[00:11:01] Okay, so let's go into, where am I right now, pwd, I'm in the root directory. I'm gonna go into home. And another thing that I meant to tell you is, I've been doing cd /home/ubuntu, which is a qualified directory. But if I go into cd home without the slash, it's relative to where I am.
[00:11:24] So here I'm saying, I know inside of here, there's something called home. Same thing here, I know there's something called ubuntu. So I don't need to put /home/ubuntu, I can do it relative to where I am. And that works as well. Okay, so I'm gonna go up one more directory here.
[00:11:45] And one more thing I'm going to show you. So if I go back in here to Ubuntu, you can go ../ and then I can do things relative to where I am in here. So if I wanted to go from here into ubuntu again, which would be pointless, could be going out and in, that would work.
[00:12:05] You can get wild here, though, right, so I can say ../ubuntu/../ubuntu. Right, so I can go out and in and out and in. That's how it works. Again, I don't know why you would want to do that, but totally possible.