How can I determine if what a process is outputting is stdout or stderr?
4 Answers
There are only three ways I know of to determine what a program will output to STDOUT and what to STDERR
Read the documentation. Or
Experiment with redirection†
†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
- 81,981
- 20
- 135
- 205
-
24I usually use a variation of option 3: `program | grep .` prints STDOUT in red. – Dennis Jul 26 '12 at 01:49
-
Printing in red is pretty much exactly what I was looking for. Thanks RedGrittyBrick & Dennis – Rauffle Jul 26 '12 at 16:53
Based on your commented request:
{ { command; } 2>&3 | sed 's/^/STDOUT: /'; } 3>&1 1>&2 | sed 's/^/STDERR: /'
- 533
- 2
- 10
-
3Looks 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
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.
- 121
- 2
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>
- 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