Linux less behavior and stderr

I'm watching the output of my complicated command with less, problem is the stderr gets lost. stderr lines normally get listed inbetween stdout lines inside less. I would like them to be printed to the console, and when I exit less, to see them there together.

I realize there might be no solution to this, I read about tee and multitee but no luck so far.


Solution 1:

Maybe

command 2> command.err | less; cat command.err; rm command.err

Addendum

Here follows a clarification for folk who neglect to carefully read the question and who didn't read the OP's clarifying comment above.

haelix pointed out:

stderr lines normally get listed inbetween stdout lines inside less

and, in a comment for early answerers, wrote:

You're telling me how to redirect stderr to stdout but that's not what I wanted. I don't want stderr to mix with stdout inside less. I would like stderr to be in the terminal when I exit less

The problem is probably platform specific, it is certainly something I have experienced on older Unix SVR4 platforms.

If, on such platforms, you do something like

 find / ... | less

any error messages (e.g. directory permissions) appear like this in less

 stdout line 1
 stdout line 2
 error message text
 stdout line 4

so that output lines are obscured by error messages.

If you refresh the page the output lines are shown correctly but you lose the error messages. When you exit less the screen is cleared except for a command prompt.

If you do something like

  find / ... 2>&1 | less

The error messages are intermingled with the standard output. Again when you exit less, the screen is empty.

If you want to first peruse only the standard output in less, then see the error messages after exiting less, you need a different solution.

That is what I was tentatively suggesting in my original, two-line answer.

Solution 2:

You have to redirect stderr to stdout:

$ ./somecommad 2>&1 | less

Check the manual for you shell (e.g. man bash.)

Solution 3:

just tell the shell to redirect fd 2 to fd 1 (stderr to stdout)

 make 2>&1 | less

Solution 4:

One thing that was lacking from all the answers so far is the reason, why this is happening. The problem here is some kind of race-condition between the process outputting stuff to stderr and less displaying output from stdout on the terminal. If less starts displaying after all output to stderr has been printed to the terminal, then less will preserve that and you can see the messages after exiting less. OTOH if less has already started displaying stuff, then error messages intermingle with less's output and nothing is preserved after less exits (because less just preserves the terminal as it was before it started and doesn't know anything about the error messages that came in between).

You can see that easily, if you do e.g.

grep foo -r /etc | less

All the "Permission denied" error messages mix up with less output and nothing will be there after you exit. If you do

grep foo -r /etc | (sleep 10; less)

all (or at least most) of the error messages have been printed to the terminal before less gets a chance to display output and you will see the error messages afterwards.

Of course, you don't usually want to wait 10 seconds before you start less, but with Linux you can also supply fractional values for the waiting time, and with fast running processes often something as little as sleep 0.1 is enough to avoid the race condition. (But, of course, if you want or have to be on the really safe side use RedGrittyBrick's solution).