previous | start | next


ECHO

Displays messages, or turns command-echoing on or off.

  ECHO [ON | OFF]
  ECHO [message]

Type ECHO without parameters to display the current echo setting.

Back to the top of this page

This little tidbit applies to XP, Vista, 2k8, and probably Windows 7 but I've never, in years and years of doing this seen it documented (as an error) or seen the workaround.

Try this command at the prompt:
echo test 2>test.txt

One might expect to see nothing on the console and then if you entered:
type test.txt
you might expect to see "test 2"

In fact, you see "test" on the console right after the echo and text.txt is empty!

What happened? The 2> is redirecting any error messages from echo test to the file test.txt. It is being interpreted as a stderr redirect and that leaves no > to send "test" into the file. Experienced programmers won't be shocked by that.

Now, how to avoid it? LOL this one is what surprised me:
(echo test 2)>test.txt

Can you believe it? That actually works! So the moral of the story is that if you are going to be echoing things to a file, you should probably enclose all of those echo commands in parenthesis as a habit.

It turns out that:
>test.txt echo test 2

is also a valid workaround since the parser takes the file name after the > and then goes back to interpreting commands.

Adding a space after the 2 or adding quotes is not really a valid workaround as that changes (and limits) the text that can be echoed.

See also:



previous | start | next