Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
1×
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.

Autoplay

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

    Cameron NokesCameron Nokes
    bashBash

    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.

    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Transcript

    Transcript

    Instructor: 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.

    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.

    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.

    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.

    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.

    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?

    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.

    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.

    Discuss

    Discuss