The Standard Streams and Redirection

The Standard Streams and Redirection

The three standard streams are communication channels between a computer program and its environment.

They are:

  • Standard Input
  • Standard Output
  • Standard Error


Standard Output

Standard output is a place to which a program or command can send information.

The information could go to a screen to be displayed, to a file, or even to a printer or other devices.

Standard Error

Commands and programs also have a destination to send error messages: standard error.

By default, the shell directs standard error information to the screen for us to read, but we can change the destination if needed!

Standard Input

Standard input is where a program or command gets its input information from. By default, the shell directs standard input from the keyboard.

The input information could come from a keyboard, a file, or even from another command!

Redirection

"redirection" describes the ways we can alter the source of standard input, and the destinations for the standard output and standard error.

Redirecting Output

The redirect output symbol (>) tells the shell to redirect the output of a command to a specific file instead of the screen.

By default, the date command will print the current date to the screen.

If we instead run date > output.txt, the output will be redirected to a file called "output.txt".

command > filename        


Note: If you redirect an output to an existing file, the contents of that file will be overwritten.


Appending

When we redirect output into a file using >, any existing contents in the file are overwritten. Sometimes this isn't what we want!

To instead keep the existing contents in the file and add new content to the end of the file, use >> when redirecting.


Redirecting Input

To pass the contents of a file to a standard input, use the < symbol followed by the filename.

For example, we could pass the contents of the chickens.txt file to the cat command using cat < chickens.txt

cat (and many other commands) are set up to accept filenames as arguments directly, but we can also redirect to standard input manually.

command < filename        


Redirecting Standard Input and Output Together

We can redirect standard input and output at the same time.

In this example, we are using cat to read in the contents of "original.txt" file and then redirecting the output to a file called "output.txt".


cat < original.txt > output.txt        


Let's take another example, we sort the contents of a file named "names.txt" and output them to a new file named "sortednames.txt".

sort < names.txt > sortednames.txt        


Redirecting Standard Error

By default, error messages are output to the screen, but we can change this by redirecting standard error.

The standard error redirection operator is 2>

If we ran a command like cat nonexistentfile (where the file really does not exist), we would see an error printed to the screen. We can instead redirect standard error to a file with cat nonexistentfile 2> error.txt

cat nonexistentfile 2> error.txt        


Note: If you want to redirect multiple standard errors to the same file use 2>> symbol to append instead of 2>.

Why 2> symbol?

Each stream gets its own numeric file descriptor, and for standard error the number is 2.


The > operator actually defaults to using 1 as the file descriptor number, which is why we didn't need to specify 1> to redirect standard output.

Similarly, the < operator uses a default file descriptor number of 0, so we don't need to specify 0< to redirect to standard input (though we can!).

Putting it all together

We can redirect multiple streams at once!

In this example, we are concatenating two files, redirecting the standard output to a file called pets.txt, and redirecting standard error to a file called error.txt

cat owls.txt cats.txt > pets.txt 2> error.txt        


Redirecting Output and Error to the same file

If we wanted to redirect both standard output and standard error to the same file, we could either do this..

ls docs > output.txt 2> output.txt        

Or we could instead use 2>&1 which is a fancy syntax for saying "redirect standard error to the same location as standard output" (or whatever has the file descriptor 1).

ls docs > output.txt 2>&1        


Note: Newer versions of bash also support a fancier syntax for redirecting both standard output and standard error to the same file: the &> notation.

ls docs &> output.txt        
ls docs &>> output.txt        

Note : When redirecting both standard output and standard error, make sure standard output comes FIRST. Always redirect standard error after standard output.

Thanks for reading : )

If you find my content helpful, let me know with your comments. Any suggestions or insights are welcome !

要查看或添加评论,请登录

Shivm Soaini的更多文章

  • Pipelines

    Pipelines

    Pipes are used to redirect a stream from one program to another program. We can take the output of one command and…

  • Working with Files

    Working with Files

    The cat command The cat command concatenates and prints the contents of files. cat filename will read the contents of…

  • The Shortcuts

    The Shortcuts

    We can speed up our command-line experience by taking advantage of the many built-in shortcuts. These shortcuts are…

  • Deleting, Moving and Copying

    Deleting, Moving and Copying

    The rm command We use the rm (remove) command to remove files from our machine. For example, rm app.

  • The Nano Text editor

    The Nano Text editor

    Nano is a simple text editor that we can access right from the terminal. It's far more accessible than other popular…

  • Making Files and Folders

    Making Files and Folders

    The touch command To create a new file from the command line, use the touch command. Provide a filename, and that file…

  • Navigating the CLI Sea

    Navigating the CLI Sea

    The root directory The starting point for the file system is the root folder. We call it the root, but its actual…

  • Getting Help in Command Line Interface

    Getting Help in Command Line Interface

    Sometimes it's difficult to wrap our head around some of the commands we encounter. In this scenario, man pages can…

  • First Step in Command Line

    First Step in Command Line

    Some Familiar Terms A Shell is a computer interface to an operating system which expose the OS's services to the human…

  • A Brief History of Linux

    A Brief History of Linux

    The world of operating systems has many residents, but they can be segregated into two tribes: The Microsoft NT…

社区洞察