How to output normal information and error messages to a file in Linux bash

Source: Internet
Author: User

Problem Description:

$ Command 2>> Error

$ command 1>> output

Is there a way to implement output stderr to the error file and output stdout to an output file on Bash's same line?

That is, how to implement stderr and stdout redirect to different files on the same line of bash?

Workaround:

Put them on the same line, command 2>> error 1>> output

However, note >> is that if the file has data, the content is added at the end of the file. > will rewrite the data that already exists in the file.

Just to accomplish the purpose, you can write 1> for > because its default file descriptor is output. So 1> and > are the same thing.

So

Command 2> error 1> output becomes command 2> error > output

Command 2>> error 1>> output becomes command 2>> error >> output

Or if you want to mix the output (stdout & stderr) to the same file, you can use the command:

Command > Merged-output.txt 2>&1

Easier to use: command &> merged-output.txt

which

2>&1 indicates that stderr (file description is 2) is relocated to stdout (file descriptor 1), which means that standard error messages are sent to the same location as the standard output information.

Additional notes:

In bash, 0, 1, 2...9 are file descriptors. 0 represents stdin,1 on behalf of stdout,2 on behalf of Stderror. 3~9 is not used and can be used for other temporary usages.

Any file descriptor can be redirected to another file descriptor or file by using the action character > or >> (append).

Usage:<file_descriptor> > <filename | &file_descriptor>

For more information, please refer to

Http://www.tldp.org/LDP/abs/html/io-redirection.html

Tips:

Linux make, there may be a lot of debugging information, if there is an error, because of too much output information, under Bash, can not find the first error in the wrong location. At this point, you can take the method above, first output it to a file, and then, in the file, find the location of the first error message.

References:

1 redirect stderr and stdout to different files in the same line of bash?

Http://stackoverflow.com/questions/7901517/how-to-redirect-stderr-and-stdout-to-different-files-in-the-same-line-of-bash

How to output normal information and error messages to a file in Linux bash

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.