33

How can I determine if what a process is outputting is stdout or stderr?

Rauffle
  • 644
  • 2
  • 7
  • 13

4 Answers4

32

There are only three ways I know of to determine what a program will output to STDOUT and what to STDERR

  1. Read the documentation. Or

  2. Experiment with redirection†

  3. print STDERR in red

†For example:

program > program.stdout 2> program.stderr

Then look at the two output files to see what the program has written to STDOUT and what it has written to STDERR.

Instead of redirection you can pipe to tee if you need output to continue to the screen as well as into a file. See https://stackoverflow.com/q/692000/477035

RedGrittyBrick
  • 81,981
  • 20
  • 135
  • 205
11

Based on your commented request:

{ { command; } 2>&3 | sed 's/^/STDOUT: /'; } 3>&1 1>&2 | sed 's/^/STDERR: /'
zebediah49
  • 533
  • 2
  • 10
  • 3
    Looks impressive. But would you mind to add the cherries to your tree, and explain what it is doing? Not everybody around here is a guru -- and your construct is quite a little complex ;) – Izzy Jul 26 '12 at 11:49
  • The brackets are for ordering. I actually got that exact form from I-forget-where, but the point is to use extra file descriptors (beyond 1=`stdout` and 2=`stderr`) to take the output of the inner set of brackets, and run `stdout` through one `sed` command, while `stderr` goes through a different one. – zebediah49 Jul 26 '12 at 22:31
  • Wow. Didn't know to use those extra descriptors. First I was a bit confused (I like to understand what I run -- and got a bit confused with the curly braces). But now it's clear -- and IMHO exactly what the OP wanted. So +1 from me :) – Izzy Jul 27 '12 at 06:06
  • This helped me. I wanted to add more data into stderr. This does that and properly outputs everything back on stdout/stderr. `{ { { { echo "stdout" ; echo "stderr">&2; } 2>&3; } 3>&1 1>&2 | awk '{print "ERROR:",$0}' 1>&3; } 3>&2 2>&1; }` – Bryan Drewery Mar 12 '13 at 12:49
1

You could just redirect stderr to a file and if anything shows up in it, it's from stderr.

e.g. ls -a 2> ls-all.txt

if there was an error for any reason sent to stderr, it will be redirected to this file.

magna_nz
  • 121
  • 2
-1

If you want to do this once off, redirect one of them somewhere else.

Example, redirecting standard out with >.

ls -al > ls-l.txt (any output here is not from stdout, if you see anything it must be stderr output)

For stderr redirection use 2>

Hennes
  • 64,768
  • 7
  • 111
  • 168
  • The process creates a constant stream of output, some to stdout some to stderr. I want to determine which is which as this output is going to the screen – Rauffle Jul 25 '12 at 22:47