Check out a free preview of the full Complete Intro to Linux and the Command-Line course:
The "Input Streams" Lesson is part of the full, Complete Intro to Linux and the Command-Line course featured in this preview video. Here's what you'd learn in this lesson:

Brian explains that stdin (pronounced standard in) is used for input streams using the 'cat' command to input the text of one file into another.

Get Unlimited Access Now

Transcript from the "Input Streams" Lesson

[00:00:00]
>> Let's talk about standard in. So the two that we've talked about so far are standard out and standard error, those are output streams, right? There's something flowing out of an output. You can also feed things into standard in. So cat has a fun ability that not only can it read files, it can have files inputed to it.

[00:00:21] So, if I put a angle bracket in the other direction, I can actually read out of a file and throw that into cat. So, if I put, what files do I have in here? New-file. So this is actually going to read out of new-file and throw that as an input into cat.

[00:00:45] And cat is basically just going to output whatever it gets. So in this particular case, this isn't very interesting because if I just do this, it's the same thing, right? Cuz that's what it does, is it reads a file. But that actually does end up working. But let's say here, what's in my ls.txt?

[00:01:14] Yeah, so I have this in here where it's just two outputs of an LS, right? Let's say I wanted to read from this file and find something in it. So you can actually do something kind of interesting here, where I can say, let's look for ls error. So I'm going to say grep, which we'll talk about here towards the end of the class, grep is just like a thing to like look for other things.

[00:01:39] So I'm gonna say grep for ls error.txt. And then I'm going to input into that ls.txt. So what this did Is it read from the file ls.txt and then it fed that into standard in which then connected into this grep program. And grep looks for this line of text right here, ls error.txt.

[00:02:11] So this is really great, if you have a program that you're gonna be giving it like a consistent amount of input, and you get sick of typing it over and over again, you can use this to just write to a file and then you can feed that into a program.

[00:02:29] I don't end up using it a lot, I'll be honest with you, but it's just good to know that that exists. More importantly, it's good to know what you can pipe from one program to another, which we'll get into here in just a second. Let's put this all together now.

[00:02:48] I'm gonna go into my Streams folder and I'm going to grep for ls error.txt. Oops, And I'm going to input into that ls.txt and then I'm going to pipe the output into grep.txt and I'm going to ignore the errors. Okay, so what the hell just happened, right? It's a little dense, but we know everything that went into this, right?

[00:03:28] So we have ls.txt, this is being read and put into standard in. That's being fed into grep, which is looking for ls error.txt. It's then outputting the standard out into grep.txt, and it's ignoring all of the errors. So if I cat grep.txt, what do you expect to find in there?

[00:03:51] The two found ls errors. Make sense? Now again, something interesting, notice that this ls error is not colorized whereas if I do this, it is colorized. That's because grep is smart enough to say, you're running this through a pipe and I'm not gonna put any colors onto this because that makes it harder for machines to parse.

[00:04:24] And that's just for human benefit. So it'll mix all the colors to make it more easy for the next thing to read. And order is not important here for this particular command that we wrote here. So you could put the standard in and standard out and standard error in any order that you want to.

[00:04:45]
>> Questions about that one character before the redirection operator.
>> All the one character, this one. I thought you'd talk about the one character, right? That one.
>> Yeah.
>> Yes, so if you put a one there, you're redirecting standard out and if you put a two there, you're redirecting standard error.

[00:05:08] Why is it a one?. Cuz it's the first. I don't know if there's a really good reason to it. It's just the way that they wrote Bash. And then again, if you leave it out it redirects both standard error and standard out.