Redirection in bash

One of the more advanced features of bash are the redirection operators. These operators allow you to divert the input or output of a command to another command or file. While this sounds simple, it is an extremely powerful feature. There are several redirection operators built into bash, as shown here:

Operator

Description

|

This is a general-purpose command that chains similar to >

>

This redirects the output of a command to a file

<

This reads a file and passes it to the command

>>

This appends the output of the command to a file

Redirection operators

The | operator is a general-purpose command-changing tool. A simple example of this is to use it with cat and the program more. The more program is a simple program that lets you move backward and forward throughout a file on the screen in order to read it. The more command gets the data that you want to read from the cat command. The | operator connects two programs together.

Redirection in bash

The more and cat commands connected using the | operator

> and >>

The > and >> operators are really powerful operators that take the data that is output by a program and write it to a file. The > operator replaces the file and the >> operator appends it to a file.

Remember that everything in Linux is a file, so you can use the > and >> operators to direct the output to a serial port or a printer.

> and >>

The > operator writes the result of an ls command to a file

<

The < operator is similar to the > operator, except that it reads the contents of a file and feeds it to a program. This can be used in a lot of ways and is in some ways similar to using <.

<

Using the < operator to send the contents of dir.txt to the more application

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset