facebook youtube pinterest twitter reddit whatsapp instagram

Pipes and Redirection in Bash

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

ls -l > out.txt

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:

ls -l >> out.txt

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:

ls -l &> out.txt

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:

command &> /dev/null

Related Post(s)

  • sed (Stream Editor) In Bash Scripting

    Another powerful filter in GNU/Linux is sed (stream editor), which is a program for performing basic editing tasks on the output of another program or on a file. sed or stream editor can be used to p

  • awk In Bash Scripting

    Up until now, we have covered a couple of text processing, from sed to grep to sort, and now we have the big daddy, which is awk. awk (which stands for Aho, Weinberger, and Kernighan - its creators)

  • (Bash Script) Automate Multiple Classicpress & Wordpress Installation + [Caching and SSL]

    If you are tired of using control panels (I am because they are mostly not secure, and most comes loaded with bundles of useless stacks), and want to do everything on the server level, I wrote a menu

  • Returning Values From Functions in (Bash)

    In the guide function in bash, we dive deep in bash functions and we also pass some information into the functions to carry out a certain operation. In this guide, I'll show you how to get informatio

  • Functions in Bash

    Functions are modular building blocks for creating powerful and modular scripts, using function makes it easy to isolate a code since it would be in a single building block, which would be isolated f

  • grep (Regular Expression) In Bash Scripting

    We recently discussed filters in bash scripting, in this guide we would look more into the practical usage of using a filter program, and an example of such a program is grep. grep, in its simplest f