yego.me
💡 Stop wasting time. Read Youtube instead of watch. Download Chrome Extension

Terminal Lesson 13


5m read
·Nov 3, 2024

That gets what I want here today. This is terminal lesson 13. Um, let's get started. Today's topic is going to be on piping.

Now, this is a little more complex. Once again, if you don't really want to learn anything, you just want to look cool in front of your friends or whatever, you don't have to watch this. This is just for your sake and mine. This is something piping and standard and put standard output.

So first of all, let's get started with every program or, you know, UNIX application. Every command basically has standard input and standard output. What standard output is, like in this case, is the terminal. Standard output is whatever gets printed out. So like if you type passwd, the thing that says “changing password for alex” gets printed out in standard output. “Old password” gets printed out in standard output.

Now, there are a few other things. Say you type a random command, hit enter. Now, there's actually the program, it's called the shell. And what this is, is the command line. Like when you open a terminal, what's running is the shell. And so this little prompt, it's getting printed out in the shell.

Now right here, we see this error that says “bash: command not found.” That's putting out through standard error. And then standard input is basically the way it's getting input. In this case, from the shell, it's from the keyboard, getting input from the keyboard whenever I type.

So, um, that's pretty simple. Anyway, there are a few other things like, um, whenever, like, you might have seen this ps aux vertical bar, you know, like grep something or whatever. grep is actually a program called grep. Like you can type it in right here and nothing will happen. Like this, nothing will come out. You type Ctrl-C.

Now, the thing is grep is just a program. They put standard input into grep, and then it takes a line. If the line contains what you've grepped, then it will print it back out through standard output. So now when you do ps aux, what you're doing is you're setting the standard input of grep as the standard output of ps aux.

So instead of printing everything that comes out here onto the console, into the terminal, it sends it right to grep. And grep then gets standard input and decides, “If this line contains what I'm grepping, I'll print it back out.” And if it doesn't, I won't print it out, and no one will ever see it.

So let's just get this clear. Normally, when you type a command, it sets the standard input to the console. So if I do grep hi then I type ‘crap,’ nothing will come out. But if I take crap hi more crap, then it'll print back out because that line contains hi. But if I type something that doesn't have hi, it won't print back out.

So basically, instead of me typing to grep, ps aux, instead of typing this to the console, it types it to grep. So that's what the vertical bar is. That's what the bar up and down is. It's really a pipe, and I guess I'll be calling it a pipe for now on.

So, um, another cool thing you can do, I haven't showed you up until now, if I just go to my desktop. If you type echo space hi, and you know what echo does. You just do echo hi, and it prints it to standard output. So print, I write out here to the terminal, right? Well, the little issue with that is, well, there's no issue with that. But what the cool thing with that is, is that you can type echo hi space > and that's actually the greater than symbol.

Now I've just printed out hi, but instead of the standard output of echo being into the console, it's to this file called hi that I made on my desktop. Open it up, and there is when I echoed, which is hi. So that's pretty cool.

So I've just basically piped echo into a file. Now I can also pipe it to ~/desktop/ty. That'll just rewrite desktop. Now whenever you do that, you're rewriting a file. So I really suggest you don't do that to a huge file just to add another line to it. If you want to append to a file, you need to >> instead of >.

So it should look something like this: echo hi >> yourfile. So now I've just added hi onto hi. Now hi has two is, you can see right here. So that's actually very useful in long terms because we just type, we can type to a file.

So you could do this with anything. You could do ps aux > ps, now instead of printing everything in ps aux to the console, to the terminal, it's printing it to my file ps right here. This is everything that would show up in a ps aux streaming right here to this file. How useful is this?

So you can do that for any command. Like if you do passwd, and this is gonna be annoying, > nothing will print out except “old password.” I think is standard error, apparently, but standard output went to passwd and it's empty. So passwd does some weird freaky things.

But say you run, let's see, something that prints something out back at you. Let's do something awful. Let's type nano ha. Nothing is going to print out. Nothing is willing to be visible. Everything that's trying to print out is going right to this file. Then I'll just press Ctrl-X, I'll guess and hit enter.

Now you can tell it just saved. Let's see exactly what happened to my file paths. This is everything that normally we'd be printing out. Didn't really get now. Hopefully append be a little different. Now I've written yes, so it has everything that got printed out at any time in nano. And that doesn't matter very much.

It's just really cool you can pipe any program's standard output to a file. You can also pipe standard error. Let's say I do find / -name hi -print. You didn't get a few errors here. Wouldn't it be great just to be able to get rid of the errors? You can! You can pipe just the errors to a file.

We do 2> errors. 2> stands for error. So 2> errors will print all the errors to a file. You can see already a few errors have been printed to this file. Open it up at any time and see any new errors.

So we don't have to get them printed on the screen. So that is, um, piping. I'm sure you have questions; if you do, just leave them in the comment box, and I will answer them. Thank you and subscribe to Mac Heads on the Left. Bye.

More Articles

View All
2015 AP Chemistry free response 4 | Chemistry | Khan Academy
Answer the following questions about the solubility of calcium hydroxide, and they give us the solubility product. Write a balanced chemical equation for the dissolution of solid calcium hydroxide in pure water. So, we’re going to start off with calcium…
Unpacking employee benefits | Employment | Financial Literacy | Khan Academy
When looking at a new job, it can be very tempting to look only at the pay. That’s because the pay is really important, and that’s the bulk of what the company is going to give you. But there are other things that they will give you beyond pay that you sh…
SELLER ACCEPTED MY OFFER - BUYING MY 6TH PROPERTY!
What’s up, you guys? It’s Graham here! So, I realized it’s been quite some time since I made a video standing up. But this is the perfect time to make a standing up video because I have some good news to share with you guys. Spoiler alert: I ended up get…
The elements of a story | Reading | Khan Academy
Hello readers! I’m going to draw you a map right now, and it’s going to look like I’ve drawn a mountain. But it’s not a map of a mountain; it’s a map of a story. What you’re saying: how do you map a story? What makes a story pointy? These are great quest…
2d curl nuance
In the last couple of videos, I’ve been talking about curl, where if we have a two-dimensional vector field v defined with component functions p and q. I’ve said that the 2D curl of that function v gives you a new function that also takes in x and y as in…
Homeroom with Sal & Marta Kosarchyn - Tuesday, June 30
To our daily homeroom live stream, I’m excited about our conversation today with Khan Academy’s Head of Engineering, VP of Engineering, Marta Casarchin. Uh, but before we get into that, I will give my standard announcement. A reminder to everyone that Kh…