steenhulthin
steenhulthin

Reputation: 4803

How do I send a message to stderr from cmd?

In a standard windows .cmd file I can send messages to stdout simply by doing:

echo message

How can I send a message to stderr?

Example:

Let's say I have the script parent.cmd containing:

call child.cmd 1>>stdout.log 2>>stderr.log

and a child containing:

:: Writes a message to stdout 
:: (which in the parent is piped to stdout.log)
echo message

:: Now I want to write a message to stderr 
:: (which in the parent is piped to stderr.log)
???

Upvotes: 85

Views: 34912

Answers (3)

Craig McQueen
Craig McQueen

Reputation: 43486

As a comment pointed out:

echo message 1>&2

would output a trailing space.

As Dúthomhas pointed out in a comment, it is possible to drop the 1 and then the space can also be dropped.

echo message>&2

Alternatively, the redirect can be put at the start of the command:

>&2 echo message

Upvotes: 1

dolphy
dolphy

Reputation: 6518

You can try redirecting the handle of STDOUT to the handle of STDERR. In your child process, perform the echo with a handle redirect:

:: Writes a message to stdout 
:: (which in the parent is piped to stdout.log)
echo message

:: Now I want to write a message to stderr 
:: (which in the parent is piped to stderr.log)
echo message 1>&2

Microsoft references:

Upvotes: 102

user7094
user7094

Reputation:

Try echo message 1>&2

I've just tried this and it seems to work correctly, even from within a batch file.

Upvotes: 28

Related Questions