Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3603562
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:53:15+00:00 2026-05-18T20:53:15+00:00

For testing purposes, I would like to save stdout and stderr separately for inspection

  • 0

For testing purposes, I would like to save stdout and stderr separately for inspection by subsequent code. For example, a test run with erroneous input should result in output to stderr, but nothing on stdout, while a test run with correct input should result in output to stdout, but nothing to stderr. The saving has to be synchronous, to avoid race conditions with the tests (so I can’t use process substitution).

To be able to debug the test after the fact, I also need to see stdout and stderr in the sequence they were output. So I have to either save them both to the same file/variable/whatever or send them to the terminal at the same time as saving them separately.

To test which error happened, I also need the exit code of the command.

For reasons of efficiency and accuracy, I of course cannot run each test twice.

Is it for example possible to redirect stdout to stdout.log, stderr to stderr.log, and both of them to output.log in the same command? Or to use a synchronous tee command separately for stdout and stderr? Or to save a copy of stdout and stderr to separate variables?

Update: It looks like tim’s solution almost works (modified to output on terminal instead of logging to all.log):

$ set -o pipefail
$ {
    {
        echo foo | tee stdout.log 2>&3 3>&-
    } 2>&1 >&4 4>&- | tee stderr.log 2>&3 3>&-
} 3>&2 4>&1
foo
$ cat stdout.log
foo
$ cat stderr.log
$ {
    {
        echo foo >&2 | tee stdout.log 2>&3 3>&-
    } 2>&1 >&4 4>&- | tee stderr.log 2>&3 3>&-
} 3>&2 4>&1
foo
$ cat stdout.log
$ cat stderr.log
foo
$ bar=$({
    {
        echo foo | tee stdout.log 2>&3 3>&-
    } 2>&1 >&4 4>&- | tee stderr.log 2>&3 3>&-
} 3>&2 4>&1)
$ echo "$bar"
foo
$ cat stdout.log
foo
$ cat stderr.log
$ bar=$({
    {
        echo foo >&2 | tee stdout.log 2>&3 3>&-
    } 2>&1 >&4 4>&- | tee stderr.log 2>&3 3>&-
} 3>&2 4>&1)
$ cat stdout.log
$ cat stderr.log
foo
$ echo "$bar"
foo

This seems to work except for the last iteration, where the value of bar is set to the contents of stderr. Any suggestions for all of these to work?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T20:53:16+00:00Added an answer on May 18, 2026 at 8:53 pm

    Please see BashFAQ/106. It’s not easy to have your cake and eat it, too.

    From that page:

    But some people won’t accept either the loss of separation between stdout and stderr, or the desynchronization of lines. They are purists, and so they ask for the most difficult form of all — I want to log stdout and stderr together into a single file, BUT I also want them to maintain their original, separate destinations.

    In order to do this, we first have to make a few notes:

    • If there are going to be two separate stdout and stderr streams, then some process has to write each of them.
    • There is no way to write a process in shell script that reads from two separate FDs whenever one of them has input available, because the shell has no poll(2) or select(2) interface.
    • Therefore, we’ll need two separate writer processes.
    • The only way to keep output from two separate writers from destroying each other is to make sure they both open their output in append mode. A FD that is opened in append mode has the guaranteed property that every time data is written to it, it will jump to the end first.

    So:

    # Bash
    > mylog
    exec > >(tee -a mylog) 2> >(tee -a mylog >&2)
    
    echo A >&2
    cat file
    echo B >&2
    

    This ensures that the log file is correct. It does not guarantee that the writers finish before the next shell prompt:

    ~$ ./foo
    A
    hi mom
    B
    ~$ cat mylog
    A
    hi mom
    B
    ~$ ./foo
    A
    hi mom
    ~$ B
    

    Also, see BashFAQ/002 and BashFAQ/047.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to display an UIView above my tableview, for testing purposes I
I'm learning XSLT and, for testing purposes, would like be able to simply display
For software testing purposes I would like to create a sterile clone (with all
For testing and simulation purposes I would like to delete a file that is
I'm starting to try and test my Doctrine objects with PHPUnit, and would like
I'm in the process of extending monkey for software testing purposes, so I would
For development and testing purposes, I'd like to install two web heads and a
For testing purposes I have to generate a file of a certain size (to
for testing purposes i need an recursive directory with some files, that comes to
For testing purposes I need to get my Outlook 2003 addin (vb.net) disabled so

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.