InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
Bash
Sending Both Output and Error Messages to the Same File in bash
Bash

Sending Both Output and Error Messages to the Same File in bash

InfinityCoder January 12, 2017

Using redirection, you can redirect output or error messages to separate files, but how do you capture all the output and error messages to a single file?

Use the shell syntax to redirect standard error messages to the same place as standard output.

Preferred:

1
$ both >& outfile

or:

1
$ both &> outfile

or older and slightly more verbose:

1
$ both > outfile 2>&1

where both is just our (imaginary) program that is going to generate output to both STDERR and STDOUT.

&> or >& is a shortcut that simply sends both STDOUT and STDERR to the same place—exactly what we want to do.
In the third example, the 1 appears to be used as the target of the redirection, but the >& says to interpret the 1 as a file descriptor instead of a filename.

In fact, the 2>& are a single entity, indicating that standard output (2) will be redirected (>) to a file descriptor (&) that follows (1).

The 2>& all have to appear together without spaces, otherwise the 2 would look just like another argument, and the & actually means something completely different when it appears by itself.

(It has to do with running the command in the background.)
It may help to think of all redirection operators as taking a leading number (e.g., 2>) but that the default number for > is 1, the standard output file descriptor.
You could also do the redirection in the other order, though it is slightly less readable, and redirect standard output to the same place to which you have already redirected standard error:

1
$ both 2> outfile 1>&2

The 1 is used to indicate standard output and the 2 for standard error.

By our reasoning (above) we could have written just >&2 for that last redirection, since 1 is the default for >, but we find it more readable to write the number explicitly when redirecting file descriptors.
Note the order of the contents of the output file. Sometimes the error messages may appear sooner in the file than they do on the screen.

That has to do with the unbuffered nature of standard error, and the effect becomes more pronounced when writing
to a file instead of the screen.

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Preventing Core Dumps in bash
You want to prevent your script from dumping core in …

Preventing Core Dumps in bash

Using SSH Without a Password in bash
You need to use SSH or scp in a script …

Using SSH Without a Password in bash

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap