Skip to content

Bat 重定向

ythy edited this page Jan 11, 2018 · 1 revision

Summary

  • STDERR: descriptor number2 the error messages are sent to Standard Error
  • STDOUT: descriptor number1 the regular output is sent to Standard Out

When you redirect console output using the > symbol, you are only redirecting STDOUT. In order to redirect STDERR you have to specify 2> for the redirection symbol.

EXAMPLE1

echo "hey" >&2

  • > redirect standard output (implicit 1>)
  • & what comes next is a file descriptor, not a file (only for right hand side of >)
  • 2 stderr file descriptor number

Redirect stdout from echo command to stderr.
If you were to useecho "hey" >2 you would output hey to a file called 2

EXAMPLE2

The command dir file.xxx (where file.xxx does not exist) will display the following output:

Volume in drive F is Candy Cane Volume Serial Number is 34EC-0876
File Not Found

If you redirect the output to the NUL device using dir file.xxx > nul, you will still see the error message:

File Not Found

To redirect the error message to NUL, use the following command: dir file.xxx 2> nul

you can redirect the output to one place, and the errors to another: dir file.xxx > output.msg 2> output.err

You can print the errors and standard output to a single file by using the &1 command to redirect the output for STDERR to STDOUT and then sending the output from STDOUT to a file:
dir file.xxx 1> output.msg 2>&1

Clone this wiki locally