Welcome to MSDN Blogs Sign in | Join | Help

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.

Published Thursday, April 03, 2008 7:00 AM by oldnewthing
Filed under:

Comments

# re: How do I force the ECHO command to echo?

Thursday, April 03, 2008 10:40 AM by KenW

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.

# re: How do I force the ECHO command to echo?

Thursday, April 03, 2008 10:47 AM by keithmo

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

# re: How do I force the ECHO command to echo?

Thursday, April 03, 2008 11:05 AM by Anonymous Coward

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).

# re: How do I force the ECHO command to echo?

Thursday, April 03, 2008 12:17 PM by Wang-Lo

How do I echo a dot?

-Wang-Lo.

# re: How do I force the ECHO command to echo?

Thursday, April 03, 2008 12:22 PM by Mark Sowul

echo .

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

# re: How do I force the ECHO command to echo?

Thursday, April 03, 2008 12:52 PM by Richard

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).

# re: How do I force the ECHO command to echo?

Thursday, April 03, 2008 1:05 PM by Anonymous Coward

Options aren't recognized after the -- because options aren't recognized after anything which isn't an option.

# re: How do I force the ECHO command to echo?

Thursday, April 03, 2008 1:10 PM by Dan Lewis

Here are some "solutions" in Solaris.

Unix echo -n echoing "echo -n\n":

_echo -n -n && echo_

Because echo is not portable, you can use printf.

_printf "-n\n"_ works in tcsh. In bash it is an error; it picks up -n as an argument.

However, _printf "%s\n" -n_ works in both.

# re: How do I force the ECHO command to echo?

Thursday, April 03, 2008 1:12 PM by Dan Lewis

And one more interesting one:

echo -n - && echo n

# re: How do I force the ECHO command to echo?

Thursday, April 03, 2008 1:36 PM by josh

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.

# re: How do I force the ECHO command to echo?

Thursday, April 03, 2008 2:13 PM by Josh

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"

# re: How do I force the ECHO command to echo?

Thursday, April 03, 2008 3:42 PM by chiph

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!

# re: How do I force the ECHO command to echo?

Thursday, April 03, 2008 6:00 PM by Michael Mrozek

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

# re: How do I force the ECHO command to echo?

Thursday, April 03, 2008 6:16 PM by Richard

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]

# re: How do I force the ECHO command to echo?

Thursday, April 03, 2008 6:19 PM by piyo

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...

# re: How do I force the ECHO command to echo?

Thursday, April 03, 2008 6:35 PM by steveg

Very handy XP cmd reference.

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

# re: How do I force the ECHO command to echo?

Thursday, April 03, 2008 9:37 PM by Daev

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?

# re: How do I force the ECHO command to echo?

Friday, April 04, 2008 12:03 AM by Yuhong Bao

"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.

# re: How do I force the ECHO command to echo?

Friday, April 04, 2008 3:02 AM by Xepol

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.

# Wackylabs.Net : How to use ECHO to output a blank line

# re: How do I force the ECHO command to echo?

Friday, April 04, 2008 4:40 AM by will

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

# re: How do I force the ECHO command to echo?

Friday, April 04, 2008 5:32 AM by Morten

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. ;-)

# re: How do I force the ECHO command to echo?

Friday, April 04, 2008 7:35 AM by Johannes Rössel

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 :)

# re: How do I force the ECHO command to echo?

Friday, April 04, 2008 12:32 PM by MWS

Thanks to MS for the PowerShell !

# re: How do I force the ECHO command to echo?

Friday, April 04, 2008 9:49 PM by Dan

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.

# re: How do I force the ECHO command to echo?

Friday, April 04, 2008 9:53 PM by Dan

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.

# re: How do I force the ECHO command to echo?

Saturday, April 05, 2008 2:23 AM by Gabe

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.

# re: How do I force the ECHO command to echo?

Saturday, April 05, 2008 8:39 AM by SRS

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.

# re: How do I force the ECHO command to echo?

Saturday, April 05, 2008 11:25 AM by Theo V

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

# re: How do I force the ECHO command to echo?

Saturday, April 05, 2008 3:37 PM by Jake

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...

# re: How do I force the ECHO command to echo?

Saturday, April 05, 2008 3:47 PM by Yuhong Bao

"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.

# re: How do I force the ECHO command to echo?

Monday, April 07, 2008 1:05 PM by 640k

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

Was that a joke to demo the sucky search engine?

New Comments to this post are disabled
 
Page view tracker