1

I'm trying to redirect stderr to stdout and then use tee to both show it on sceen and dump to a file.

When running a command with a typo (to which cmd reacts with 'CommandNameWithATypo' is not recognized as an internal or external command, operable program or batch file.) I expect this error message to be shown on screen and also written to the file specified in the tee command. However, running this:

CommandNameWithATypo 2>&1 | tee test.txt

I get no output on screen nor to a file (no file is created), as if piping never happened. Is this intended to be like this? May there be a workaround?

SiLiKhon
  • 583
  • 4
  • 15
  • you *do* have `tee` installed, don't you? – Stephan Jan 13 '18 at 12:25
  • @Stephan yes, and it does work for cases like `echo blabla | tee file.txt` – SiLiKhon Jan 13 '18 at 12:37
  • 1
    try `( CommandNameWithATypo 2>&1 ) | tee test.txt` to bind `2>&1` to the left side of the pipe – Stephan Jan 13 '18 at 13:37
  • @Stephan Yes, that works! Thanks a lot. Do you know what was exactly was happening inside the interpreter when not using parenthesis? P.S. If you post this as an answer I'll mark it as the best one. – SiLiKhon Jan 13 '18 at 16:30

1 Answers1

2

the problem is, that "cmd knows best where to put redirection" /for prove, write a batch file with >nul echo hello (don't turn echo off), run it and watch the output. You can force it to bind redirection to the intended part of the code with a code block:

( CommandNameWithATypo 2>&1 ) | tee test.txt 

(sorry for "you do have tee installed, don't you?", but without tee, you get exactly what you described: nothing. Try wrongCommand 2>&1 | notTee file.txt)

Stephan
  • 53,940
  • 10
  • 58
  • 91
  • PS: if you really want to crawl through it, start [here](https://stackoverflow.com/q/4094699/2152082). Be warned: this is "the land of headache" – Stephan Jan 13 '18 at 17:39