Pipes and Redirection in Bash

Last Updated on: August 6, 2020

Piping is the process where the result of one command is sent or pipe (redirect) into another command. A good example of this is piping the output of ls command into wc (count) for counting the number of files.

On the other hand, redirection captures output from a file, command, script, etc, and sending it as input to another file, command, program, or script. It mostly works with a stdin (Standard Input), stdout (Standard Output), and Standard error.

Before a command is executed, its input and output may be redirected to a file, this is useful in gaining an idea of what is going on, and also a clear idea of why an error occurs if they actually occur in the process of the command being executed.

By default the stdout and stderr descriptors are “redirected” to the screen, meaning that the program output and its errors are not saved to any file, but just displayed to the screen, redirection let us change that.

Let’s try an example:

Redirecting Output

The above command would redirect the output of the ls command to the file out.txt, you don’t have to create the file, it would do it for you automatically.

Now if you check the out.txt, you would see the output of the ls command.

Did you see the ‘>’ operator, this represents the output redirection.

If you try the redirection again inside the out.txt, the current content of the out.txt file will be replaced by the new output. To preserve the content and just append new lines to it, you use the >> operator, e.g:

Redirect Standard Error

The standard error or stderr is the default error output device, and can be your screen. To redirect an error, you would do the following:

					

cp file /home/user 2> error.log

You might wonder why we have number 2 and a >, the number 2 is a file descriptor, don’t get it twisted, a file descriptor is a number that uniquely identifies an open file in a system, so by using 2, it knows you are referring to the stderr. In short, if you use 1> it would be the same as >, which redirects to the standard output.

Redirect Both Standard Output and Standard Error

To redirect both standard output and standard error to a single file, you do:

This would redirect both the output and error of the command in a single file, it can also be done like this:

ls -l > out.txt 2>&1 – This is the old way of doing it, 2 refers to the second file descriptor of the process (stderr), > means redirection, &1 means the target of the redirection should be the same location as the first file descriptor (stdout)

Redirect To null device

If you want to prevent any output or error in your stderr, you can redirect to /dev/null, which is also called the black hole as it discards anything written to it and only returns an end-of-file EOF when read, do the following:

Comment policy: Respectful and beneficial comments are welcome with full open hands. However, all comments are manually moderated and those that doesn't relate with what the passage is saying or offensive comments would be deleted. Thanks for understanding!

Leave a Reply

Your email address will not be published. Required fields are marked *