Shell Input/Output redirection

Source: Internet
Author: User
Tags stdin

Shell Input/Output redirection

Most UNIX system commands accept input from your terminal and send the resulting output back to?? to your terminal. A command usually reads the input from a place called the standard input, which, by default, happens to be your terminal. Similarly, a command usually writes its output to standard output, which is also your 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).

Output redirection

Redirects are typically implemented by inserting specific symbols between commands. In particular, the syntax for these symbols is as follows:

Command1 > File1

The above command executes Command1 and then stores the output in File1.

Note that any content that already exists within the file1 will be replaced by new content. If you want to add new content to the end of the file, use the >> operator.

Instance

Execute the following who command, which redirects the full output of the command to the user file (users):

$ who > users

After execution, there is no output information in the terminal, because the output has been redirected from the default standard output device (terminal) to the specified file.

You can use the Cat command to view the contents of a file:

$ cat Users_mbsetupuser console    to :  tianqixin    console  :  tianqixin    ttys000  Dec  1:

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

" Rookie Tutorial: Www.runoob.com  > users$ Cat users Novice Tutorial: www.runoob.com$

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

" Rookie Tutorial: Www.runoob.com  >> users$ Cat users Novice Tutorial: www.runoob.com Rookie Tutorial: www.runoob.com$

Input redirect

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

Command1 < File1

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 (<).

Instance

Next, we need to count the number of rows in the users file and execute the following command:

$ WC-L users       2 users

You can also redirect the input to the users file:

$  wc-l < users       2

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.

Command1 < infile > outfile

Replace the inputs and outputs at the same time, execute Command1, read the contents from the file infile, and write 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 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:

2 > file

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

2 >> file

2 indicates a standard error file (stderr).

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

2>&1

Or
2>&1

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

$ command < File1 >file2

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

Here Document

Here Document is a special redirection in the shell used to redirect input to an interactive shell script or program.

Its basic form is as follows:

Command << delimiter    documentdelimiter

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.
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.comEOF3          3  lines $

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

#!/bin/<< EOF Welcome to rookie tutorial Www.runoob.comEOF

Execute the above script and output the result:

Welcome to Rookie Tutorial Www.runoob.com

/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:

$ 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:

$ command >/dev/null2>&1

Note:0 is the standard input (STDIN), 1 is the standard output (STDOUT), and 2 is the standard error output (STDERR).

List of Notes

  1. 2>&12>&1

    The & here has no fixed meaning.

    & on the back of > , indicating that the redirect target is not a file, but a file descriptor, with the built-in file descriptor as follows

    1 = stdout2 = stderr0 = stdin

    In other words 2>1 delegates redirect stderr to the regular file named 1 under the current path , while the 2>&1 represents stderr redirection to a file descriptor of 1 of the files (i.e. ,/dev/stdout), this file is the mapping of stdout in the file system

    And &>file is a special use, can also be written >&file, the meaning of the two are identical, are equivalent to

    2>&1

    Here &> or >& as a whole, separate no separate meaning

    Order problems:

    2>&12>&1 > List

    This is from left to right in order.

    First Kind

    2>&1

    The content to be output to stdout is redirected to a file, at this point the file list is the program's stdout, and then redirect stderr to stdout, which is the file list

    The second Kind

    2>&1 > List

    The content to be output to stderr is redirected to stdout, which produces a stdout copy, which is the stderr of the program, and the contents of the program to be exported to stdout are still docked in StdOut, so the second step redirects stdout, without any effect on the stdout copy.

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.