Shell Input/Output redirection

Source: Internet
Author: User

Most UNIX system commands accept input from your terminal and send the resulting output back to?? to your terminal. commands usually read input from the standard input, which is the default terminal . Standard output, which is the terminal by default.

The list of REDIRECT commands is as follows:

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.

Note that the file descriptor 0 is typically standard input (STDIN), 1 is the standard output (STDOUT), and 2 is the standard error output (STDERR).


Input redirect
Instance

Following the example above, we need to count the number of rows in the users file: $ WC -l users

       2 users

You can also redirect input to the users file:$ WC -L < users

       2

Note: The results of the above two examples are different: The first example will output the file name; the second one will not

 <> outfile   simultaneously replaces inputs and outputs, executes Command1, reads the contents from the file infile, and writes the output to outfile .
Redirect in-depth explanation

In general, each Unix/linux command will open three files when it is run:

    • standard input file (stdin): stdin has a file descriptor of 0, and UNIX programs read data from stdin by default.
    • standard output file (STDOUT): StdOut has a file descriptor of 1, and UNIX programs output data to stdout by default.
    • standard error file (stderr): stderr has a file descriptor of 2, and the UNIX program writes an error message to the stderr stream.

After merging stdout and stderr, redirect to file: $ command > file 2>&1
Command commands redirect stdin to File1, redirect stdout to File2: $ command < file1 >file2 /c9>

Here Document

A special redirect in the shell used to redirect input to an interactive shell script or program. Pass the contents (document) between the two delimiter as input to command.

<<  delimiter    documentdelimiter

Note: the end of the delimiter must be shelf written, cannot have any characters , including spaces and tab indentation. The space before and after the beginning of the delimiter is ignored.

Instance

The number of rows in here Document is computed by the wc-l command at the command line:

$ WC   - l  < <   EOF   Welcome to    rookie tutorial   www  .  runoob   comeof  3# output is 3 rows $           

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

#!/bin/bash<< EOF Welcome to Rookie Tutorial www.  Runoob.  comeof        

Execute the above script and output the result:

Welcome to the  Rookie tutorial www.  Runoob.  COM     
/dev/null file

If you want to execute a command, but do not want the output to be displayed on the screen, the content that is written to it will be discarded :

>/dev/null 

The/dev/null file is useful to redirect 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:$ command > /dev/null 2>&1

Shell Input/Output redirection

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.