How do I force the ECHO command to echo?


The ECHO built-in command, how much simpler could it get? It takes whatever you put on the command line and prints it. And yet it's not that simple.

For example, the ECHO must be careful not to compress whitespace, because people will write

ECHO Some text
ECHO    Indented text
ECHO             ----     underlined

and when you execute this, the result had better be

Some text
   Indented text
            ----     underlined

and not

Some text
Indented text
---- underlined

But what if you want to echo a blank line or the word "ON" or "OFF" or a slash and a question mark?

C:\> ECHO ON

C:\> ECHO
ECHO is on.
C:\> ECHO /?
Displays messages, or turns command-echoing on or off.
...

To force the ECHO command not to interpret its arguments, put a dot immediately after the word "echo":

C:\> ECHO.    ON
    ON
C:\> ECHO.

C:\> ECHO./?
/?

This is what happens when a language develops not by design but by evolution. It becomes filled with all sorts of strange quirks in order to accommodate new behavior while remaining compatible with old behavior. Nobody actually likes the batch language; they just are used to it.

Comments (32)
  1. KenW says:

    Strange. In another forum this morning we were discussing another change added to CMD.EXE (delayed variable expansion). Someone was complaining about the way variable expansion worked in an IF <cond> {} block, and then complained that it had been fixed but required the /V command line parameter to CMD.EXE when starting it.

  2. keithmo says:

    Kenw: You can use "SETLOCAL ENABLEDELAYEDEXPANSION" to enabled delayed environment variable expansion within a batch file. No "/V" required.

  3. Anonymous Coward says:

    The problems with "echo" are not exclusive to Windows. autoconf, for instance, has to do several tests to check for various quirks of the "echo" command on ancient Unix versions.

    And the Unix "echo" also has a similar problem to "ECHO ON": if you try "echo -n", for instance, it will "eat" the -n argument (unless you are using some of the quirky Unix versions).

  4. Wang-Lo says:

    How do I echo a dot?

    -Wang-Lo.

  5. Mark Sowul says:

    echo .

    (I can’t believe I had to write that)

  6. Richard says:

    Or ‘echo..’.

    In general, "echo.<string>" seems to print <string> unmolested.

    I was surprised to find that there’s no equivalent for this functionality in /bin/echo, nor in the echo builtin in bash and zsh. It appears that it can be faked up by "echo — ‘<string>’ | sed ‘s/^– //’" (weirdly, options aren’t recognised after –, but the — itself gets echoed).

  7. Anonymous Coward says:

    Options aren’t recognized after the — because options aren’t recognized after anything which isn’t an option.

  8. Dan Lewis says:

    Here are some "solutions" in Solaris.

    Unix echo -n echoing "echo -nn":

    echo -n -n && echo

    Because echo is not portable, you can use printf.

    printf "-nn" works in tcsh. In bash it is an error; it picks up -n as an argument.

    However, printf "%sn" -n works in both.

  9. Dan Lewis says:

    And one more interesting one:

    echo -n – && echo n

  10. josh says:

    You can break "ECHO." by creating a file of the same name.  I use "ECHO:", which works as well.  Partly because I’m paranoid and partly because I think it looks better.

  11. Josh says:

    I like PowerShell’s solution:

    "anystring"

    To force a quoted string to be interpretted as a command instead of an expression, prepend and ampersand:

    & "C:some command.exe"

  12. I always used echo. to output a blank line, i never realized you could include arguments that wouldn’t be parsed like in echo

  13. Richard says:

    I occasionally want to output a <, >, or | character as part of the echo string, and have found no easy way to do it.  Putting double quotes around it prints the double quotes (ugly).  Leaving them off produces a syntax error as these are piping/redirection characters.  I’m guessing & has a similar problem in Windows NT+.  Anybody know a way to output these “meta” characters in echo?

    [I wonder what happens if you search for the word metacharacter. -Raymond]
  14. piyo says:

    I didn’t know about the dot trick, but thanks to this blog I remember the ‘echo ^>^>^> your message indented’ trick. Without the carets you get the ‘> was unexpected at this time’. Cryptic and temporally strange, heh.

    Where can I look up this stuff? On XP, ‘hh ntcmds.chm’ gets me started…

  15. steveg says:

    Very handy XP cmd reference.

    http://www.ss64.com/nt/

  16. Daev says:

    A very, very good book on how to do nearly everything with the batch file language is Tim Hill’s "Windows NT Shell Scripting."  If you ever wondered how to implement associative arrays or get guaranteed-correct filename quoting in BAT files, that’s the place to look.  (And yes, he covers the ECHO. trick as well.)  I’ve come to really appreciate the power of the classic Windows script language.  Who needs Powershell?

  17. chiph says:

    I knew about the dot ‘trick’.  There *is* an advantage of having started working with DOS 3.0 in the dim mists of time after all!

  18. Yuhong Bao says:

    "This is what happens when a language develops not by design but by evolution. It becomes filled with all sorts of strange quirks in order to accommodate new behavior while remaining compatible with old behavior. Nobody actually likes the batch language; they just are used to it."

    That is exactly what the PC BIOS is. It is now being replaced by EFI, and it is already on the Intel-based Macs.

  19. Xepol says:

    Funny, I always liked dos batch file language.  Heck, I even liked Edlin…

    I combined them together once to create a multi-user email system to emulate something I saw on a mainframe at the time.

    Of course, it was less useful since only one person could log into the PC at a time.

    Still, it was just as capable as the unix script version.  The only annoyance was getting dates and times formatted the way I wanted in the mail headers.

  20. will says:

    Or if you are using powershell just do a echo "." or echo "       string"

  21. Morten says:

    I have to object to Raymond’s last comment – I like the batch language. It’s quirky and cranky and you have to twist your brain 90 degrees to get what you want instead of what you ask for. It’s a real challenge. And I don’t ever depend on it for survival – 4NT r00lz. ;-)

  22. Ok, that definitely was new to me. I remember having used echo. for emitting a blank line but never thought about appending a string to print.

    This makes my bresenham implementation much prettier :)

  23. MWS says:

    Thanks to MS for the PowerShell !

  24. Dan says:

    I’ve known about the dot trick for awhile.

    I tried it in a few other internal commands.  With dir the dot is always treated like a space, as it is with cls and probably most other commands.

    With cd it’s treated like a space and a dot (cd. acts like cd . instead of just cd, cd.. acts like cd .., cd.<name> gives a not found error, etc).  CD’s behavior is probably because people kept typing cd.. and when it didn’t change directory they reported it as a bug.

    Oddly "cd …" or with any more dots doesn’t do anything on XP.  You’d think it’d at least throw an error.  I think on one OS or DOS/Windows shell or something … means two directories up, …. meant three, etc.  Not sure where I remember that from, maybe 4DOS.

  25. Dan says:

    Well… I just did some more testing… interestingly it seems <command>./<anyvalidswitch> always works (including on CD) but other usages treat . as part of the parameter.

  26. Gabe says:

    I seem to recall hearing that CD.. worked as a result of a really old bug in COMMAND.COM. Of course it proved to be quite a handy shortcut, and that special case had to be preserved ever since.

  27. SRS says:

    Dan: cd … did exactly what you described (move two dirs up) in DOS’s command.com.

    …and I remember a time when the @ command prefix wasn’t supported, so the first line in every batch script was echo’d. Sigh.

  28. Theo V says:

    For ages I am now using echo+ to output a blank

    line, works from dos 6.22 to win23.  I vaguely

    remember that echo. didn’t always work

  29. Jake says:

    I miss "cd…", so much simpler than "cd ….".

    Alas, it was one of those useful Win9x features that never got ported to the NT line and subsiquently died with Win9x, like the toolbars on command windows and the simple password protection for file shares…

  30. Yuhong Bao says:

    "simple password protection for file shares."

    That feature in Windows 9x, which came from WfW, was not ported to NT for good reason. NT’s security is much better than this feature.

  31. 640k says:

    [I wonder what happens if you search for the word metacharacter. -Raymond]

    Was that a joke to demo the sucky search engine?

Comments are closed.