Standard IO and redirection

Source: Internet
Author: User

1. Standard input/Output/error

When the shell starts, it inherits three files: stdin, stdout, stderr, standard input usually comes from the keyboard, standard output and standard error are usually screens.
Standard input/output/error file descriptors are 0, 1, 2

2. REDIRECT Operator

< REDIRECT input

<& redirect input, followed by a number format file descriptor

> REDIRECT Output

>> Append Output

2> REDIRECT Error

&> redirect error and output, followed by file name

>& redirect error and output, followed by file descriptor for file name or number format

2>&1 REDIRECT standard error to standard output

1>&2 REDIRECT standard output to standard error

>| Overwrite noclobber when redirecting output, that is, if file exists directly overwrite file

<>filename if it is a device file, use this file as standard input and standard output

3. Example

1) redirect standard error to file
$CC prog.c 2>errfile

2) redirect standard output to file, standard error redirect to empty device file
$CC prog.c >logfile 2>/dev/null

3) direct standard output and standard error to file or file descriptor
$CC prog.c &> logfile//must be a file name
$CC prog.c >& logfile//Can be file descriptor in file name or number format
$CC prog.c >& 1//redirect standard output and standard error to standard output, file descriptor for standard output is 1

4) redirect standard output to file, redirect standard error to standard output directed device
$CC prog.c >logfile 2>&1
is equivalent to
$CC prog.c >& LogFile

=-=-=-=-=
Powered by Blogilo

Standard IO and redirection

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.