When we type something into our terminal program, we’ll often see output. For example: Show
As we can see, Standard outputEvery Unix-based operating system has a concept of “a default place for output to go”. Since that phrase is a mouthful, everyone
calls it “standard output”, or “stdout”, pronounced standard out. Your shell (probably bash or zsh) is constantly watching that default output place. When your shell sees new output there, it prints it out on the screen so that you, the human, can see it. Otherwise Standard inputStandard input (“stdin”, pronounced standard in) is the default place where commands listen for information. For example, if you type
As you can see, with standard input, you can send a string to a command directly. PipesPipes connect the standard output of one command to the standard input of another. You do this by separating the two commands with the pipe symbol (
Hey, if sed sends its result to standard out, can we pipe sed to another sed? Yep!
Above, we’ve connected echo to sed, then connected that to another sed. Pipes are great for taking output of one command and transforming it using other commands like jq. They’re a key part of the Unix philosophy of “small sharp tools”: since commands can be chained together with pipes, each command only needs to do one thing and then hand it off to another command. Standard errorStandard error (“stderr”) is like standard output and standard input, but it’s the place where error messages go. To see some stderr output, try catting a file that doesn’t exist:
Hey, that output looks just like stdout! But wait a moment. Let’s try transforming that output with pipes:
Whoa - nothing changed! Remember, pipes take the stdout of the command to the left of the pipe. We need to redirect Redirecting outputBy default, stdout and stderr are printed to your terminal – that’s why you can see them at all. But we can redirect that output to a file using the
The second
So if
File descriptorsA file descriptor, or FD, is a positive integer that refers to an input/output source. For example, stdin is 0, stdout is 1, and stderr is 2. Those might seem like arbitrary numbers, because they are: the POSIX standard defines them as such, and many operating systems (like OS X and Linux) implement at least this part of the POSIX standard. To duplicate output to a file descriptor, use the
This is very similar to redirecting output to a file, as we did above, but you can think of stdout and friends as special files that
require us to use Visually all of the output above looks the same, but the changes become apparent when we start piping output. Let’s see what happens when we redirect to stdout versus when we redirect to stderr:
ZSH users, take note!If you’re using ZSH, you may have already noticed slightly different results. This is due to ZSH’s Here’s what the output of ZSH with the
For this blog post, I’ll assume you’re using bash, or have ZSH with the Advanced file descriptorsLet’s say you have stderr output mingled with stdout output – perhaps you’re running the same command over many files, and the command may output to stdout or stderr each time. For convenience, the command outputs “stdout” to stdout, and “stderr” to stderr, plus the file name. The visual output looks like this:
We want to transform every line to have “Robot says: ” before it, but just piping the command to sed won’t work, because (again) pipes only grab stdout:
This is a common use case for file descriptors: redirect stderr to stdout to combine stderr and stdout, so you can pipe everything as stdout to another process. Let’s try it:
It worked! We successfully redirected stderr (FD 2) into stdout (FD 1), combining them and sending the combined output through stdout. Common use casesWe could further redirect that combined output to a file, to review the combined output later at our leisure:
Neat: we’ve captured both stderr and stdout in a file. One interesting point is that we need to do this:
and not this: The correct version points stdout at the log file, then redirects stderr to stdout, so both stderr and stdout point at the log file. The wrong version points stderr at stdout (which outputs to the shell), then redirects stdout to the file. Thus only stdout is pointing at the file, because stderr is pointing to the “old” stdout. Another common use for redirecting output
is redirecting only stderr. To redirect a file descriptor, we use We can use this new syntax to silence stderr by redirecting it to
Excellent. Further readingPeteris Krumins wrote an excellent blog post about bash redirection and goes into more detail on custom file descriptors. If you’re a little rusty on Joël wrote a blog post on file descriptors and what they map to in “IO in Ruby”. And if you want your very own
What command redirects the standard output to a file?Append Redirect shell command
The >> shell command is used to redirect the standard output of the command on the left and append (add) it to the end of the file on the right.
Which operator is used to redirect output to an existing file?The append >> operator adds the output to the existing content instead of overwriting it. This allows you to redirect the output from multiple commands to a single file.
Which symbol is used to redirect the output in a file?Redirecting output to append to a file
When the notation > > filename is added to the end of a command, the output of the command is appended to the specified file name, rather than writing over any existing data. The >> symbol is known as the append redirection operator.
Which operator is called as output redirection operators?So, what we learned is, the “>” is the output redirection operator used for overwriting files that already exist in the directory. While, the “>>” is an output operator as well, but, it appends the data of an existing file. Often, both of these operators are used together to modify files in Linux.
|