Redirection in the shell (input and output)

Source: Internet
Author: User
Tags stdin

Note: Different versions of Linux will differ, but they are very similar

The Linux command obtains the input from the standard input device (stdin) By default, outputting the result to the standard output device (STDOUT) display. In general, the standard input device is the keyboard, the standard output device is the terminal, that is, the display.

Output redirection

The output of the command can not only be a monitor, but it can also be easily transferred to a file, which is called output redirection.

The syntax for command output redirection is:

    1. $ Command > file

In this way, the contents of the output to the display can be redirected to the file.

For example, the following command will not see any output on the monitor:

    1. $ who > users

Open the users file and you can see the following:

$ cat Usersoko         tty01   Sep 07:30ai          tty15 Sep 13:32ruth tty21   Sep 10:10pat         tty24   Sep 13:07steve       tty25   Sep 12 13:03$

Output redirection overwrites the file contents, see the following example:

$ echo Line 1 > users$ cat usersline 1$

If you do not want the file content to be overwritten, you can use >> to append to the end of the file, for example:

$ echo Line 2 >> users$ cat usersline 1line 2$
Input redirect

As with output redirection, Unix commands can also get input from a file with the following syntax:

    1. Command < file

In this way, commands that would otherwise need to get input from the keyboard are transferred to the file read content.

Note: Output redirection is greater than sign (>), and input redirection is less than sign (<).

For example, to calculate the number of rows in the users file, you can use the following command:

$ wc-l users2 users$

You can also redirect the input to the users file:

$ wc-l < users2$

The WC command can output the number of bytes in the specified file, the word book, the number of lines

Note: The results of the above two examples are different: The first example outputs the file name, and the second one does not because it only knows what to read from the standard input.

Redirect in-depth explanation

In general, each Linux command will open three files when it is run:

    • Standard input file (stdin): stdin file descriptor for 0,unix program reads data from stdin by default.
    • Standard output file (stdout): StdOut file descriptor for 1,unix program outputs data to stdout by default.
    • Standard error file (stderr): The stderr file descriptor for the 2,unix program writes an error message to the stderr stream.


By default, command > file redirects stdout to File,command < file to redirect stdin to file.

If you want stderr to be redirected to file, you can write:

    1. $command 2 > file

If you want stderr to append to the end of file, write this:

    1. $command 2 >> file

2 indicates a standard error file (stderr).

If you want to redirect stdout and stderr to file after merging, you can write:

    1. $command > File 2>&1

Or

    1. $command >> file 2>&1

If you want to redirect both stdin and stdout, you can write this:

    1. $command < file1 >file2

The command commands redirect stdin to File1, redirecting stdout to File2.

Output standard or standard output or standard error files can be entered into file. You can write this:

    1. $command &>>File



List of all redirected commands available
Command Description
Command > File Redirects the output to file.
Command < file Redirects the input to file.
Command >> file Redirects the output to file in an append manner.
n > File redirect files with file descriptor N to file.
n >> File Files with file descriptor n are redirected to file in an append manner.
N >& m Merges the output file m and N.
N <& m Merges the input file m and N.
<< tag Enter the contents of the tag between tag and end tag tags as input.
Here Document

Here document does not currently have a unified translation, which is now translated into "embedded documents". Here Document is a special redirection method in the Shell, which has the following basic form:

    1. Command << delimiter
    2. Document
    3. Delimiter

Its role is to pass the contents (document) between the two delimiter as input to the command.

Attention:

    • The end of the delimiter must be shelf write, the front can not have any characters, the back can not have any characters, including spaces and tab indentation.
    • The space before and after the beginning of the delimiter is ignored.


In the following example, the number of document rows is computed by the WC-L command:

$WC-L << EOF    A simple lookup program for    Good (and bad) restaurants in    Cape town.eof3$

You can also use here Document in a script, for example:

    1. #!/bin/bash
    2. Cat << EOF
    3. A simple lookup program
    4. For good (and bad) restaurants
    5. In Cape town.
    6. Eof

Operation Result:

This was a simple lookup programfor good (and bad) Restaurantsin Cape town.


The following script saves the document to the Test.txt file through the VI editor:

    1. #!/bin/sh
    2. FileName=test.txt
    3. VI $filename <<endofcommands
    4. I
    5. This file is created automatically from
    6. A shell script
    7. ^[
    8. Zz
    9. Endofcommands

To run the script:

$ sh test.shVim:Warning:Input is not from a terminal$

Open Test.txt, you can see the following content:

$ cat test.txtthis file was created automatically froma shell script$
/dev/null file

If you want to execute a command, but you do not want the output to appear on the screen, you can redirect the output to/dev/null:

    1. $ Command > /dev/null

/dev/null is a special file, and the content written to it is discarded, and if you try to read from the file, nothing is read. However, the/dev/null file is very useful and redirects the output of the command to it, which results in a "no output" effect.

If you want to block stdout and stderr, you can write this:

    1. $ Command > /dev/null 2>&1

Redirection in the shell (input and output)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.