Piping both stdout and stderr in bash?

(Note that &>>file appends to a file while &> would redirect and overwrite a previously existing file.) To combine stdout and stderr you would redirect the latter to the former using 1>&2. This redirects stdout (file descriptor 1) to stderr (file descriptor 2), e.g.: $ { echo “stdout”; echo “stderr” 1>&2; } | grep -v … Read more

How do I write to standard error in PowerShell?

Use Write-Error to write to stderr. To redirect stderr to file use: Write-Error “oops” 2> /temp/err.msg or exe_that_writes_to_stderr.exe bogus_arg 2> /temp/err.msg Note that PowerShell writes errors as error records. If you want to avoid the verbose output of the error records, you could write out the error info yourself like so: PS> Write-Error “oops” -ev … Read more

redirect stdout/stderr to a string

Yes, you can redirect it to an std::stringstream: std::stringstream buffer; std::streambuf * old = std::cout.rdbuf(buffer.rdbuf()); std::cout << “Bla” << std::endl; std::string text = buffer.str(); // text will now contain “Bla\n” You can use a simple guard class to make sure the buffer is always reset: struct cout_redirect { cout_redirect( std::streambuf * new_buffer ) : old( … Read more

Python read from subprocess stdout and stderr separately while preserving order

The code in your question may deadlock if the child process produces enough output on stderr (~100KB on my Linux machine). There is a communicate() method that allows to read from both stdout and stderr separately: from subprocess import Popen, PIPE process = Popen(command, stdout=PIPE, stderr=PIPE) output, err = process.communicate() If you need to read … Read more

Shell redirection i/o order

The Bash manual has a clear example (similar to yours) to show that the order matters and also explains the difference. Here’s the relevant part excerpted (emphasis mine): Note that the order of redirections is significant. For example, the command ls > dirlist 2>&1 directs both standard output (file descriptor 1) and standard error (file … Read more

Bash script – store stderr in a variable [duplicate]

Try redirecting stderr to stdout and using $() to capture that. In other words: VAR=$((your-command-including-redirect) 2>&1) Since your command redirects stdout somewhere, it shouldn’t interfere with stderr. There might be a cleaner way to write it, but that should work. Edit: This really does work. I’ve tested it: #!/bin/bash BLAH=$(( ( echo out >&1 echo … Read more