Understand how to Redirect stdin, stdout, and stderr in Bash

Cameron Nokes
InstructorCameron Nokes
Share this video with your friends

Social Share Links

Send Tweet
Published 6 years ago
Updated 6 years ago

Maybe you’ve seen something like 2>&1 at the end of a bash command before. What does that even mean? In this lesson, we’ll learn what stdin, stdout, and stderr are. From there, we’ll see that stdin, stdout, and stderr can be referenced by their file descriptors (0, 1, and 2) and are all just streams that can be directed however we want.

Instructor: [00:00] We can actually verify the file descriptors for standard in, standard out, and standard error. We'll list out our OS's device folder, dev stands for device. Then we'll grip. You see here standard in is pointing to file descriptor zero. Standard out, file descriptor one, standard error pointing to file descriptor two.

[00:19] All bash commands will output their data to two streams. Standard out and standard error. To redirect standard out we use the right angle bracket. It redirects our list here. We do that. Then if we cat LS.TXT we can see here's the one file I had in this folder. Then LS.TXT, it didn't exist before and it was created on the fly before this LS occurs. It shows up here.

[00:49] This works just the same if we do LS file descriptor one. Now we're explicitly referencing standard out here. We do the same thing, we get the same output. You can think of this as a short hand for this. You can see here we're redirecting standard out to file instead of letting it show up in our terminal, which is the default standard out.

[01:11] If we run LS on a directory that doesn't exist, which throws an error, we'll see what happens. We'll do LS no exist and then as a redirect standard out to LS.TXT. We see that this is standard error here and this is not redirected because we wouldn't expect to see this on our terminal if it was being sent to this file.

[01:31] How do we redirect standard error? Let's do LS no exist. We'll do file descriptor two and send that to LS.ERRORS.TXT. Note here we're only redirecting a standard error. This is throwing an error. We cat this out. You see that it works. Here's a normal error message and it's being captured in this file now.

[01:56] If we did this on just general directory, we would see it here because we're not redirecting standard out two. How do we redirect both the standard error and standard out to the same file, which is a common use case?

[02:14] Let's do LS no exist again and we'll redirect standard out to LS.TXT. We'll do file descriptor two. Then ampersand file descriptor one. The ampersand here is going to tell bash that this redirection is not going to a file, it's going to a file descriptor. Let's run that. Now if we cat LS.TXT we can see that's where our standard error went. Then if we change it to not have an error and we cat that again, we see there's our normal standard output.

[02:50] How do we redirect standard in? For example, instead of doing cat and passing a file, I can redirect standard in with the left angle bracket and I'll send LS.TXT to it. We can see that it works just the same. I haven't found a lot of use cases for redirecting standard in because most bash commands accept both text or files as an input, but there you have it.