Check out a free preview of the full Developer Productivity course

The "CLI Piping" Lesson is part of the full, Developer Productivity course featured in this preview video. Here's what you'd learn in this lesson:

ThePrimeagen discusses how CLI piping works and then live codes a program that reads from the standard in, writes to the standard out, and takes in a line. The built program will read out lines as it experiences them to demonstrate pipes.

Preview
Close

Transcript from the "CLI Piping" Lesson

[00:00:00]
>> All right, next, so before we get started we kind of need to cover a little bit of like groundwork because I don't know like I said,. I don't know how experienced you are with the terminal but I know that you're somewhat experienced at this point we've cut in typing some commands.

[00:00:13]
So let's just talk about piping. Piping is something that we do often and you may not realize you're doing it. Let's talk about this right now. So I'm gonna jump over to a little, temporary folder really quickly, you're probably like how did he just do such a quick navigation?

[00:00:25]
Well, that's what we're building right now, let's do this. I'm going to create a file called pipe.js, right? I assume we're all fairly familiar with JavaScript. I won't even use TypeScript keeping it superduper simple here, very, very simple. So what I want to do is I want to show you the value or how piping works.

[00:00:44]
So you've seen these commands before, right? Where you have some program a bar, another program, a bar another program, right? You've probably typed this into your terminal you probably cut and paste it before. If you remember when I did let's see. Let's go like this curl. There you go look at that.

[00:01:01]
Are you gonna you're gonna pipe all these things I thought I had something with. What did I do? I thought I had one inside my ansible. Ansible them slash my there we go. Okay, I curl and I pipe it into sh right here. So it's something we do all the time.

[00:01:16]
We may not realize we do this all the time, right? But we actually do do it all the time. So let's go back. And what I want to what I want to show you is like kind of how that works, right? So let's reopen a vim let's look at pipe.

[00:01:29]
Now, I always forget how to read from the standard in. So read standard in, all right? That's how we do that. Grab that, go down here, yank that, all right, awesome. And now let's paste that in. So that there we go we're reading from the standard in. So if you don't know what the standard in is, let me show you.

[00:01:47]
Also I hopefully you're like how did he get all that information so quickly? Show you, don't worry, we'll get there. We're getting there. Okay, I wanna, I'm leading you, I want you to be excited about what we're learning right now. So anytime you run a program from the terminal, what is really is happening, and this is in some crude sense, obviously this is not 100% true.

[00:02:05]
It's just sort of true, right? You have a program that runs and it gets standard in into it. Now if you're typing and pressing Enter, have you ever used a program where you actually have to type into it from the command line. That's because it's taking your terminal and the input coming in from your terminal and piping it into the program.

[00:02:21]
And the stuff that it produces or writes out to your terminal, it's coming out of the standard out, the stdout. There's also another one called the stdair. We don't need to worry about that right now. So if you do the pipe operator say we do A, B what's really happening is that the outright here from a is actually being directly inputted much like your input from the terminal going into A will be piped into B,, right?

[00:02:46]
So that out from A is going into the in from B and the out from B is going into the terminal. And so that's kind of like what's happening with piping and to make it really obvious. We're gonna build a little small program right now together. We're gonna build a program that will read from the standard in and write to standard out, and it's gonna take in a line.

[00:03:07]
Now the line, let's say it's a bunch of space delimited numbers and we wanna sum them and we want to write it back out. Actually, let's keep that previous line. So let's like this, let's go line.split( )reduce(sum, x). Some plus x, but we need another plus because we got to convert it to a number, right?

[00:03:33]
Look at that. That's just love JavaScript. There we go. We get that. And there we go. We've now done everything but there we go beautiful. So now we've done everything we've taken a line split by whitespace and summed every component casting every single token into a number. Awesome.

[00:03:50]
So if we provided a list of numbers, it's going to provide us the sum back out, and we're going to read it from the line. So let's put one more little thing right here. We'll just go like this how about line. Awesome. So let's close that. And now we need to feed it some lines.

[00:04:05]
So we can go like this, node pipe, right? And so to kind of prove the point of this picture that what you write into the terminal goes into the program, I'm gonna go like this. I'm gonna go 5, 6 or 7, 9, press Enter. It just told me back out, line 21.

[00:04:24]
The output from A is going into my terminal the input from me is going into A, right? So let's close this thing down. And instead, let's use echo, 5, 7, 9 new line 11, 13 ,15. Why all odd numbers I don't know and let's pipe it into node pipe dot j s.

[00:04:40]
So it's gonna run, it's gonna sum each line as it experiences it. And so that's what just happened right there. Notice that A piped into B be piped into my terminal. So hopefully you kinda get this idea of pipes. I just really wanna make sure that no one feels left behind.

[00:04:54]
As we do these things or when they see the pipe operator, it's not like some magical. There's nothing magical about it right? It's actually a pretty simple item and it makes a lot of sense. Alright, awesome. I'm sure there's better ways of explaining it. That's just my best way of explaining it, writing a little program seeing how it is because we're about to do some programming.

[00:05:11]
And it's gonna be an everyone's favorite language bash, which is a write once language. You write it once you enjoy it, it breaks, you look at it, you delete it and you write it and go. So it's a right one's kinda language, but we're gonna start with bash because you should know a little bit of bash.

[00:05:26]
I like to write enough bash to shoot myself in the foot. I feel like that's the proper amount of bash.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now