Ignoring the output of a command

Earlier, I used >nul 2>&1 to suppress the output of a command, and the question was brought up as to why this was "better" than just >nul.

To understand what's going on, you need to know that there are two streams of output from any given command.  There's STDOUT (or "standard out", or "standard output") and STDERR (or "standard error").  Well-behaved executables will output error messages to STDERR and regular output to STDOUT.  In CMD by default, both STDOUT and STDERR are echoed to the console.  You can redirect this output by using >.  Typically this is done to redirect output to a file.  However, this only redirects STDOUT to the file; STDERR will still display on the screen.

On the command line, when redirecting output, 1 (or nothing) means STDOUT and 2 means STDERR.  That is, foo.exe >foo.txt redirects foo.exe's STDOUT to foo.txt, and its STDERR (if any) will display on the screen.  Running foo.exe 1>foo.txt would do the same thing.  To deal with STDERR, use 2>, as in foo.exe 2>foo.txt

So, if you wanted all output from foo.exe, you could run foo.exe >foo.txt 2>foo.txt.  As it happens, since that's a lot of typing, CMD has this little shortcut for tying one stream's redirection to another: foo.exe >foo.txt 2>&1 means "Run foo.exe and redirect its STDOUT to foo.txt, and redirect its STDERR to wherever STDOUT goes."

This is all well and good for redirecting to files, but there are also some built-in "devices" which act like files.  NUL is one such "device", which is used to just discard any output.  So running foo.exe >nul 2>&1 would discard any and all output from foo.exe.  Since devices act like files, you'll notice you can't create files that are the same name as a device.

For those of you who are keeping score at home, other output devices off the top of my head include:

  • LPT1, LPT2, etc. (parallel ports, usually used for Line PrinTers)

  • COM1, COM2, etc., (serial ports)

  • AUX (auxilliary device - COM1 by default)

  • CON (console - the default device, i.e., your screen)

  • PRN (printer - same as LPT1, though something in the back of my mind tells me there used to be a way to reassign this).

Edit: Reformatted for clarity.

Comments (2)

  1. guowu says:

    Just discovered your blog. Information like this is great. Keep up the good work!

  2. Earlier, I tried to come up with a definitive list of illegal filesystem names, but it seems this is…

Skip to main content