Sabuncu
Sabuncu

Reputation: 5264

What does an echo followed immediately by a slash do in a Windows CMD file?

When examining this Windows batch/CMD file (RefreshEnv.cmd from Chocolatey source), I came across the following line of code:

echo/@echo off >"%TEMP%\_env.cmd"

The above creates file _env.cmd with content "@echo off".

What does the slash do in "echo/"? If I leave out the slash, the resulting file is exactly same.

I am on Windows 7.

Upvotes: 2

Views: 2164

Answers (1)

aschipfl
aschipfl

Reputation: 34919

At first, for your command line, there is absolutely no difference whether you are writing echo/@echo off or echo @echo off, there is just the text @echo off echoed.

A / character immediately following the echo command is sometimes used to echo an empty line, since echo without any argument returns a message like ECHO is on|off.. A very common way to do that is to write echo., although this could fail in case there is a file called echo. (no file name extension) in the current working directory, which would then be executed unintentionally.


In general, there are the following characters in cmd which separate tokens on the command line, like the command and its arguments: SPACE, TAB, ,, ;, =, the vertical tabulator VTAB (code 0x0B), the form-feed FF (code 0x0C) and the non-break space NBSP (code 0xFF). The most commonly used token separator (which I strongly recommend) is a SPACE. Multiple adjacent separators are treated as a single one.

There are some more characters which seem to separate a command from its arguments: ., :, /, \, [,], + and (, although these characters seem to become part of the first argument then. (Try type+file.txt to type the content of +file.txt but not of file.txt.)


echo however seems to behave differently though, because not all token separators are handled equally, and the aforementioned additional characters that separate the command from its arguments are not treated as part of the arguments. This is how echo behaves:

  • echo followed by nothing, or followed by a SPACE, a TAB, a VTAB, a FF or a NBSP, or a string composed by multiple such characters returns ECHO is on|off.;
  • echo followed by a ,, a ; or an = returns an empty line;
  • echo followed by a ,, a ; or an =, followed by one or more token separators returns these token separators;
  • echo followed by a token separator, followed by a string containing at least one character other than SPACE, TAB, VTAB, FF and NBSP returns that string;
  • echo followed by ., :, /, \, [,], + or ( returns an empty line;
  • echo followed by ., :, /, \, [,], + or (, followed by anything else, even token separators only, returns the said anything;
  • echo followed by ., [, ] or + fails if there is a file named echo., echo[, echo] and echo+, respectively;
  • echo followed by \ or : could also fail due to similar (but more complicated) path conflicts;
  • echo followed / cannot fail due to path conflicts, but it fails if the following text begins with ?, because /? is noticed, so the help text is displayed in the console rather than the given text;
  • echo followed by (, followed by an arbitrary text safely returns the said arbitrary text;

So the only safe way to echo any arbitrary text is to use echo(. For returning an empty line I prefer to use echo/, because it does not look that odd as echo(.

There is a great thread on DosTips about how to securely return an empty line by the echo command and how to use the echo command safely.

Regard that characters like ^, &, %, !, ), <, > and | need to be escaped properly to be returned correctly by echo; this is however necessary because of the command interpreter cmd and has nothing to do with the echo command.

Upvotes: 7

Related Questions