Learn Linux Bash Output Redirection and Command Pipes

In this tutorial we are going to learn How to use Output redirection and Pipes in Linux Bash environment.

In Linux bash there is Two types of Outputs, stdout (Standard Output) and stderr (Standard Error).

  • Standard Output also known as stdout is the output of a Linux command or program. For example stdout of the ls command is the List Files and Directories.
  • Standard Errors - Commands also give errors. For example, if you run the ls command on a file that do not exist, it will give an error message 'No such file or directory'.

Also note that one command can output both stdout and stderr at the same time. For example, You are trying to concatenate two files 'txt1'and 'txt2' with cat command, but the file 'txt2' does not exist on the system.

cat txt1 txt2
This is TXT 1
cat: txt2: No such file or directory

As per the preceding example, cat command first reads the 'txt1' and output its content "This is TXT 1". Then it will try to read txt2 and send the error "No such file or directory" since txt2 not exist.

By default both Stdout and Stderr sends to the Linux terminal.

Bash Output Redirection Operators

Following table shows operators used in Linux output redirection.

Operator Redirection Type
> Redirect stdout to a file.
>> Redirect stdout to a file, append to current file instead of overwriting.
2> Redirect stderr to a file.
2>> Redirect stderr to a file, append to current file instead of overwriting.
&> Send both stdout and stderr to one file.
2>&1 Redirect stderr to the same location as stdout.
| Send output of a one command to another command as input.
|& Send both output and errors of a one command to another command as input.

Redirect Standard Output to a File

Using bash output redirection we can send both stdout and stderr to a file. Redirection of stdout is controlled by ">" the greater-than symbol.

For example, following command will redirect the output of the ls command to file called list.txt:

ls -l > list.txt

Echo "Hello World" to a Text File:

echo "Hello World" > hello.txt

As you can see, to redirect the output we use the greater than operator (>). If the file does not exist, it will be created, and if it exists, its content is overwritten with the output of the Command.

The double greater-than sign (>>) append the output to the file instead of overwriting.

ls -l >> list.txt

Note that we are only redirecting Standard Output, any error message still goes to the Linux terminal window.

Send Standard Error to a File

Standard Errors represented by No '2', so we use 2> operator to redirect stderr.

cat file1 nofile 2> error.txt

As per the preceding example, any error message will redirect to the error.txt file.

The '2>>' operator will append stderr to the file.

cat file1 nofile 2>> error.txt

Redirect Both Standard Output and Standard Errors

We can redirect output and errors into two different file or to the same file.

Following example will redirect stdout to the stdout.txt file, Errors will send to the stderr.txt file.

cat file1 nofile > stdout.txt 2> stderr.txt

To redirect both outputs to the same file we use '&>' operator.

cat file1 nofile &> output.txt

Following Format also can be used (The order matters):

cat file1 nofile > output.txt 2>&1

This tells cat command to send stdout to output.txt followed by '2>&1' operator tells that errors also should be redirect to the same location as stdout (output.txt).

Send stdout and stderr to /dev/null

The /dev/null is a special file in Linux, Anything sent to the /dev/null is going to disappear. Anything we don't want to see, we send it to the /dev/null.

This is mostly used in Linux shell scripting where we don't want any output.

Examples

Run find command to search for passwd file in /etc directory and send Standard errors to /dev/null:

find /etc -name passwd  2> /dev/null

Search for the passwd file, output sends to the output.txt and standard errors to /dev/null:

find /etc -name passwd > output.txt 2> /dev/null

Send both stdout and stderr to /dev/null:

systemctl restart httpd > /dev/null 2>&1

Redirection with Pipe and pipelines

Another powerful bash feature is that the output from one command can be used as the input to another command, This is done by using Pipe (|) symbol. This is often used with the Linux grep command to filter the output.

In the following example, we pipe the output of the ls to grep command:

ls -l /usr/bin/ | grep python

If the directory list is too long to fit on the screen, we can pipe its output to the less command:

ls -l /usr/bin/ | grep python | less

Even though it looks simple, Output Redirection is quite useful feature in bash, especially when you write shell scripts.