Skip to playerSkip to main content
  • 5 months ago
Get a clear, hands-on look at Linux's standard input (STDIN), output (STDOUT), and error (STDERR) pipes! In this video, we explore how programs communicate using these essential streams, with practical examples in Yasm Assembly (no assembly knowledge needed!). Learn to redirect outputs, handle errors, and read inputs like a pro. Whether you're a beginner or seasoned coder, this fun demo breaks down the basics of Linux pipes. Subscribe for more coding insights and check out the full tutorial on our site!

Introduction to STDIN, STDOUT, STDERR 00:00:00
Overview of Pipes in Linux 00:00:07
Explanation of File Descriptors 00:01:12
Demonstrating Echo Command 00:01:24
Redirecting Standard Output 00:02:02
C++ Example with cout and cerr 00:02:48
Purpose of Separate Error Pipe 00:03:28
Assembly Program Introduction 00:04:55
Creating Data Section in Yasm 00:05:22
Writing Helper Functions 00:07:37
Printing to Standard Output and Error 00:11:29
Testing Output Redirection 00:12:43
Standard Input Explanation 00:14:54
Creating Stack Buffer for Input 00:16:03
Reading from Standard Input 00:17:28
Piping Input to Program 00:20:54
Handling Null Terminators 00:22:02
Conclusion and Call to Subscribe 00:23:53

Thanks for watching!

Find us on other social media here:
- https://www.NeuralLantern.com/social

Please help support us!

- Subscribing + Sharing on Social Media
- Leaving a comment or suggestion
- Subscribing to our Blog
- Watching the main "pinned" video of this channel for offers and extras

Transcript
00:00Hey everybody in this video I'm going to talk to you a little bit about standard
00:04input standard output and standard error three pipes that every program has in
00:08Linux that allow it to just communicate in a very standard way I'm going to show
00:13you some example code written in Yasm assembly but assembly knowledge is not
00:18necessarily required for this video you can just sort of follow along if you're
00:22interested in learning more about assembly see my other videos
00:30anyway what is standard input standard output and standard error well for
00:37starters every program has you know every program that gets launched under the
00:42hood the operating system will typically attach three pipes to it basically one
00:48for standard input one for standard output and one for standard error standard
00:52input is where you can send input into the program pretty easily so let's see
00:59oh I know what to do there's a there's an echo command right so here's the
01:03program called echo when the program launches it's going to have three pipes
01:08attached to it just like I told you before these pipes also behave as file
01:12descriptors as if you had opened a file but their file descriptor numbers are
01:16always zero and one and two zero for standard input one for standard output and
01:21two for standard error so if I launch the echo program it's going to get those three
01:25pipes those three file descriptors and right away the standard output is going to
01:31get attached to the program running our terminal here well not necessarily the
01:36actual terminal but bash under the hood which is the program that we're using to
01:41interact with the operating system so basically the program launches and based
01:45on this incoming argument that I give it hello then it's going to print the string
01:49hello right but what it's actually doing is it's printing to its standard output
01:53pipe its standard output file descriptor descriptor I can test this for sure by
01:58just sort of doing the command again and then muting its standard output pipe with
02:03this little shell trick I'll do a arrow or a greater than angle bracket and I'll
02:09just send the first pipe I'll do one I'll send it to dev null and on Linux dev null is
02:16just if you send anything there it just it just goes away it's just totally
02:20ignored so if I send the pipe labeled number one to just nowhere then notice how it
02:27doesn't show on the terminal for me right it's pretty cool let's go a little bit
02:33deeper into this I just want you to see real fast before I write an example
02:37program in assembly what you would probably end up doing in C++ so we usually do
02:42something like IO stream and your main you know whatever return zero and maybe we'll
02:48say using std cout std c error std and l so forth and then if you actually want to
03:00print a message to the user we'll say cout this message prints to std out because
03:08that's what cout does by default and then if we do c error a lot of people haven't actually
03:15seen this and I personally always forget that it's even there if you print to c error it prints
03:21a message to standard error why would you want two different pipes that printed messages and one
03:28is called output and one is called error well like I've said before in other videos it's really
03:33convenient to be able to have programs automate other programs for example if you have a program
03:38that is launching you know a second program maybe the second program fails in some way or wants to
03:44complain to you in some way it would usually if it's smart print the complaint to standard error it
03:51may not actually use c error because maybe it's not a c++ program but it would print to pipe number two
03:56file descriptor number two standard error and then that way the program that's launching it can just sort
04:01of ignore messages that happen on standard output because we can assume all of those messages are
04:06just normal info messages that a human might want to see or maybe you want to record to a log or
04:12something somewhere and just kind of save it away but the error messages are a little bit more
04:17important than the regular you know normal messages so if you're automating another program
04:22then your program probably wants to take standard output and either ignore it or just kind of send it
04:27to a different file and then it wants to take standard error and pay more attention to it write
04:31it to a log file save it maybe check that it's actually blank and if it's not empty then maybe alarm
04:38bells go off something went wrong we have to alert someone or you know make a database entry send an email
04:43whatever and yeah just pipe one and pipe two is how you accomplish that so this is what's happening in C++
04:53when you do that sort of thing okay so now I would like to write a sample program that just kind of
04:59demonstrates this real fast so please bear with me I'm going to go really fast for the assembly portion
05:04because this is not an assembly video if you want to learn more about how to write assembly in the first
05:10place then see my other videos for now I'm just going to create a little blank file here let's see
05:16where the heck is my window I'm on a multi-window system here okay so I'm just going to paste a little
05:22data section this is Yasm assembly a little data section I'm just going to fill it up with strings
05:26I'm going to make a string for the introduction hello the module has started a string announcing
05:31everything that we're going to do we're going to begin printing to standard output and standard error
05:35and then some messages that will go to each and then a standard input string which we'll talk about
05:41after the standard output and standard error so just a bunch of strings I'm going to make a buffer size
05:47I'm going to make system call codes for reading writing and exiting if you want to know more
05:53about system call codes see my other videos and some file descriptors this is exactly what I just
05:59talked about the file descriptor for standard input is zero for standard output is one for standard
06:04error is two and then I'm going to do a system exit code of zero which actually doesn't matter that
06:10much but I'm going to do it so now I'm going to make a let's see a global
06:21make a global no did I already make the text section here we go so there's the text section
06:31so the data section is over now I'm going to make the text section I'm going to make a global entry
06:36point called start again if you want to know more about assembly programming in general see my other
06:41videos but so this is the entry point of our program at the very end we should probably just program
06:47that it exits with success and test it out real fast so basically right now this program does nothing
06:52let me just double check clear and make run I have a make file under the hood but this is not a make
06:59file video if you want to know more about make files see my other videos basically it just starts
07:04running and then it just dies right away okay no problem let me actually can I want to fix that
07:10make file because it's it's labeled after like a different project that I was doing let's do
07:16there's no need for me to even do this I'm just uh I just can't help myself okay so there we go
07:25all right so now going back to the editor we have a program that does nothing I'm going to copy
07:33paste in a couple of useful functions that I use uh elsewhere again this is not an assembly video
07:40uh so so you can look at my code if you want to if you just kind of want to know how this works but
07:45um not the point of this video so I'm not going to spend too much time I'll just maybe I'll just
07:50briefly talk about what it does but not the point of this video so um the first thing I'm doing is I'm
07:55adding a function called print null terminated string just you know it sweeps through a string or
08:01actually it asks another function I made to find out how long the string is and then it'll print
08:05the string to some file handle that I gave it like standard output or standard error and um you know
08:12it calls on this other function called string length uh which I just wrote in pure assembly for fun
08:17even though I didn't need to uh and it just kind of sweeps through the string until it finds a zero
08:22and then it knows how long the string is and then crlf just prints a carriage return new
08:27line uh carriage return new line just um with the same idea with the print null terminated string
08:33function and you can see all my strings have null terminators that's the dot zero at the very end
08:37of it okay so we got those helper functions uh the next thing that I need to add in here is
08:44oh let me do an intro like my growling I used to have a professor who would go
08:55and he would go oh god when something went wrong and I do that all the time even when things are
09:03going right okay so I'm going to paste a little function here called intro and this is not going
09:09to be anything important it's just going to print a little hello message you know it just uses my
09:13intro string and then just prints it with those other functions I made so that means now my my our
09:18program can actually do something I can call uh intro real fast and then it'll return when it's done
09:23if I just sort of run the program again um you can see after all the build messages it's just printing
09:31that the module has started okay so nothing nothing super important yet
09:35now what I'd like to do is uh capture some incoming arguments real quick because um
09:44hmm let's see standard input well that's actually not going to work
09:50let's see what do I want to do here no I think that's part of a different program okay
09:55I'm going to call a demo a demo I don't think I need to add that one thing that I was just looking
10:01at so I'm going to call a function that's going to be our main demo so it's going to jump down
10:06somewhere I'll just create the function uh down later so let's see um where is it standard input
10:15so I'm going to start off by saying this is my standard input function
10:20uh which is this and um is this pop p and then pop r12 and then it returns if you don't know what a
10:35prologue and epilogue are or why I'm doing pushes and pops just uh see my other videos but basically
10:41I'm going to be messing around with r12 so I'm responsible for preserving it and uh I'm going to
10:47use the base pointer to help me remember where the stack started I'm just going to choose to uh
10:53what did I do
10:59I started the wrong function I'm going to do that one later okay for now
11:03let me go find where that other one is oh there it is should have double checked I'm going way too
11:09fast I'll just copy paste this whole thing from my solution so I'm going to do standard
11:15this video is a mess I'm honestly it's a mess but that's okay I'm going to try my best to explain
11:24it so I have this function standard output standard error and I'm calling it here so it should jump
11:29right down there as soon as the program starts the standard input one it's not getting called yet but
11:34we'll call it at the end of this video basically first I'm going to use my print null terminated string
11:41uh function to print a message to standard output remember standard output was just you know pipe
11:50one standard error is going to be pipe two so if I go back here and I say I want to print this
11:57matches to that pipe then it'll go to standard output just like the echo program was doing when
12:02we looked at it a moment ago and then next we're going to print to uh
12:05um oh that was just an announcement that we're going to begin so this is the real message that
12:13will say this is definitely going to standard output and then this other message is going
12:18to go to standard error which is when we uh when we looked at c error or pipe number two um a moment
12:24ago so basically I'm just printing an announcement and then two important messages one is going to pipe
12:29one one is going to pipe two for standard output and standard error okay like really not very complicated
12:35at all right so if I run this program now hopefully I didn't crash it by copy pasting out of order here
12:42yeah there we go clear and make run so uh notice how it says that the uh the messages are going to be
12:48printed and then notice that both of my messages actually show up on the terminal the one that says
12:53this message will print to standard output number one pipe one this message will print to standard error
12:58pipe two they both get printed to the terminal because by default the terminal will just take both of
13:04those pipes it'll take one and two standard output and error but I can use those little redirection
13:10tricks to mute one or more of those pipes so I'm going to say pipe one which is standard output I'm
13:18just going to redirect that to dev null to say I don't want to see standard output if I run it again
13:22notice how the only thing you can see on the string or on the screen is standard error pipe two file
13:28descriptor two on the other hand if I redirect file descriptor two pipe two to nothing then I should
13:34see all the normal messages and not see the error message notice right here it's it prints all the
13:41regular you know make file stuff and then the welcome message and then here's our message that says
13:46this is going to be printed to standard output so notice how the error message is missing
13:52so cool if I mute both of those I can do that I can say one directs to dev null and also two directs
13:59to dev null then nothing gets printed at all because that's all we really have is standard output and
14:05standard error all right not too bad right that's the basic idea between redirecting and things you
14:13could also do things like this if I okay this is not part of my plan but suppose I wanted to take
14:17any error message and redirect it to instead of dev null I wanted to redirect it to some kind of a
14:23file I could do that too I could say hello let's say standard error.txt notice how nothing seems to
14:31have shown up on the screen but if I list the directory now there's a text file called standard error
14:38that contains the error message so you can redirect pipes you can reattach them if you're like an elite
14:44hacker you can do lots of things with them but um that's as far as I'm going to take it right now I'm
14:50going to remove that now let's look at standard input how do you send input to the program remember
14:57when programs call other programs they can send each other standard input too it's just that when I launch
15:02something on the on the terminal I'm just you know I'm the human and so whatever I type or you know what
15:08command I type is going to go into standard input if I do it the right way so um let's go back to the
15:13program here okay so let's finish up standard input and um let's see the pre-made code that I had
15:21here I had the prologue and the epilogue right move the base pointer rbp and r12 oh I got to put the
15:31base pointer back into the stack pointer let's see move again this is not a an assembly video so just
15:39try to hang on here so what I'm going to do in this video I thought I was cute when I was making this
15:43plan I know I'm not like that clever but um I thought hey wouldn't it be fun instead of having
15:48a global variable as our buffer like in the in the data section or the bss section what if I just made
15:54the stack a buffer this is kind of what you do when you make a local array in c++ you're making a buffer
15:59on the stack so this is not a stack pointer uh video so long story short I'm just going to keep
16:05track of where the stack pointer started with this base pointer that's why I have to preserve it there
16:09um and then I'm just going to subtract my buffer size buffer size from the stack pointer
16:16and what is the buffer size I've chosen to do a buffer size of about eight kilobytes so that means
16:21the user or me I could send about eight kilobytes maybe eight kilobytes or less into standard input
16:27and and be sure that it's all actually going to be received by the program and then uh I'm just going
16:32to use r12 to remember where the start of the buffer is which is just wherever the stack pointer started
16:37because you know if we subtract and then like uh the data that increases is actually increasing
16:43in memory but but making room on the stack decreased it's just just the way it works there's going to be
16:47another stack video out there anyway for now I just create a buffer on the stack and then I'm going to
16:53print a little hello message so I'm going to print this so just my message saying hey we're about to do
16:59something from standard input and then I'm going to send that to a standard output so the message is
17:03going to go to standard output but I'm announcing that we're about to read from standard input
17:08assuming I actually cleaned up correctly let me double check this this should work and not actually
17:14take in any input it should just simply uh say that it's going to without crashing okay looks good
17:23so then now I'm ready to actually read from standard input we're going to use a system call from this if
17:30you want more information on system calls see my other videos but basically you just go to the system
17:36call codes table uh you know look up appendix area of whatever textbook you're using and you say you
17:42know what's the code to read what you know I want the system to read so I define that as system read and
17:49if you look up here in my program it's just call code zero so sys call code zero is to read from somewhere
17:55and then uh where do I want to read from that's the second argument for the standard uh or for the
18:01system call I want to read from standard input that's going to be the number zero if you recall
18:06and then here is a pointer to the first character of wherever I want it to to read into so it's going
18:14to read from that pipe but it has to it has to write the data that it read somewhere so that I can save it
18:19and access it so r12 is basically going to be a bunch of free bytes that I created on the stack so
18:26I like you know I'm I like made the stack bigger but it's mostly empty or with junk data and r12 is
18:33just pointing to the first byte that I have reserved so you know you could imagine maybe you make a global
18:38variable in the bss section up at the top you know like an eight kilobyte array of bytes and then just
18:44sort of point it to that but in my case I'm just using the stack because I think it's cooler so I
18:49tell it hey that's the first byte on the stack that you want that I want you to start reading from
18:53and then I give it the buffer size which we just looked at and then I do a system call and then the
18:57system now should do all the hard work for me it'll look at the pipe for standard input it'll grab all of
19:03it and then it will only grab you know up to buffer size number of characters it'll stop if the input ends
19:10before that and then when I'm done I should be able to actually read from the buffer which is sitting
19:14at r12 and print it to the screen so the next thing I'm going to copy paste in there is I'm going to
19:21print what I just read from standard input so this is another call to my function my little doggy wants
19:30to go pee even though he's completely lying he just went pee so at this point he wants to go outside and
19:37lift his leg and do nothing and then come back in and demand a treat I'll let him do it in a few
19:44minutes just in case but trust me he's lying he's laid like five times today anyway I sometimes I
19:50regret teaching him to growl at me it's so cute but like it's not cute right now I don't know maybe it
19:55is let me know in the comments um so basically I'm going to take that r12 buffer that I just wrote to
20:01and I'm going to print that directly to standard output and this is not really the important part of the
20:05video so um I'm going to do that and then I'm going to kind of clean up here at the end of the
20:11function and then uh maybe I'll say goodbye I think I just added a string for that so maybe I'll say
20:17goodbye I'll say goodbye before we end the function and then I'll actually return so let me uh go here
20:23and uh run the program let's see begin
20:28nothing actually printed because we didn't send anything and we also didn't call that let me uh
20:37call standard input call stdin and so now it's actually going to print basically nothing
20:45oh I didn't send it anything so it's just kind of hanging you probably want to check uh uh this
20:51sort of situation uh but that's that's not the point of this video let me show you how to pipe some
20:57some stuff into standard input real fast I'm going to go echo hello and then just put a little or bar
21:03in the shell this will take the output of echo and instead of printing it to the terminal it'll attach
21:08its standard output into the standard input of make run pretty cool shell trick so now we should see
21:14the word uh the word hello printed and that's what it does we have detected the following stdin
21:20and it says like here's begin that's just a string that i had that i print every time and then here's
21:26the string that it actually received and then it ends pretty nice i think maybe we could probably do
21:32the end of the empty thing if i just started typing here and then maybe i hit ctrl d to detach the
21:39buffer and yeah that's what it does it uh sort of prints here and then it uh ends there
21:48didn't do a new line i think it probably doubled it for some reason because i
21:55let's see i didn't plan for this let me see
21:59it printed oh because i did i did not null terminate the string so it actually what it did is it uh
22:05let's see if we can do that real fast one more time i'm going to go one two three four five
22:08then i'm going to just disconnect the standard input buffer because the printing function needs
22:13a null terminator to know when to stop there's not going to be a null terminator because i just typed
22:18and disconnected the uh the input so it's actually going to just continue to print through memory
22:24until it randomly finds a zero and then it'll stop so if i disconnect it it uh
22:34that explanation was bunk let me try that one more time i'm going to go a bunch of h's
22:39i guess there is a null terminator right disconnect it no no see how yeah these little symbols there
22:46so it does kind of print until it finds a null terminator but i guess i was confused because
22:51it seemed like it printed it twice what's actually happening is when i type then it's getting sent to
22:57the terminal which and that's not part of the program that we just wrote then when i disconnect
23:02standard input then the program finally prints everything that i actually typed
23:06until it hits a null terminator in this case it looks like it seems to have worked
23:11but uh if i just do like a couple sevens and then disconnect
23:15probably have to do this a bunch of times before we'll see those weird symbols again
23:20but what did i do hit enter that seems to have worked my dog is stressing me out but i love him
23:27all right anyway just trust me on this sometimes it's hitting the null terminator in time sometimes
23:32it's not but i hope by now you understand the difference between standard input standard output
23:38and standard error thank you so much for watching this video that i'm recording and i hope you learned
23:45a little bit of stuff and had a little bit of fun i'm gonna let my dog lie to me for a second and i'll
23:50see you in the next video hey everybody thanks for watching this video again from the bottom of my
23:57heart i really appreciate it i do hope you did learn something and have some fun if you could do me a
24:02please a small little favor could you please subscribe and follow this channel or these videos or whatever
24:09it is you do on the current social media website that you're looking at right now it would really mean
24:13the world to me and it'll help make more videos and grow this community so we'll be able to do more
24:18videos longer videos better videos or just i'll be able to keep making videos in general so please
24:24do do me a kindness and uh and subscribe you know sometimes i'm sleeping in the middle of the night
24:30and i just wake up because i know somebody subscribed or followed it just wakes me up and i get filled with
24:34joy that's exactly what happens every single time so you could do it as a nice favor to me or you could
24:40you control me if you want to just wake me up in the middle of the night just subscribe
24:43and then i'll i'll just wake up i promise that's what will happen also uh if you look at the middle
24:49of the screen right now you should see a qr code which you can scan in order to go to the website
24:53which i think is also named somewhere at the bottom of this video and it'll take you to my main website
24:58where you can just kind of like see all the videos i published and the services and tutorials and things
25:03that i offer and all that good stuff and uh if you have a suggestion for uh uh clarifications or errata
25:12or just future videos that you want to see please leave a comment or if you just want to say hey
25:16what's up what's going on you know just send me a comment whatever i also wake up for those in the
25:21middle of the night i get i wake up in a cold sweat and i'm like it would really it really mean the world
25:27to me i would really appreciate it so again thank you so much for watching this video and um enjoy
25:34the cool music as as i fade into the darkness which is coming for us all
25:57so
26:04so
26:06so
26:08so
26:12so
26:18so
26:33so
26:39so
26:45so
27:05so
27:11so
27:38so
28:05so
28:07so
28:11so
28:15so
28:17so
28:21so
28:25so
28:31so
28:42so
28:44so
28:48so
28:52so
28:54so
Comments

Recommended