2

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.

Sabuncu
  • 5,095
  • 5
  • 55
  • 89
  • 1
    It is just a character as any else. You might see `echo.`, `echo(` and others as well. Afaik... More can be read [here](http://ss64.com/nt/echo.html) under "ECHO a variable" and "ECHO a new line". – geisterfurz007 Jan 06 '17 at 22:01
  • 2
    Well IMO it is a convenience to use a different char from a space as a space followed by an eventually empty %var% causes an `echo is on` reply. I remember some people made comparisons which other chars are possible and have an excution speed difference. –  Jan 06 '17 at 23:43
  • @LotPings What is %var% in this context? Thanks. – Sabuncu Jan 07 '17 at 01:50
  • Just the explanation why some people don't use the space as the command seperator with the echo. –  Jan 07 '17 at 02:17
  • @LotPings OK thanks, understand you now. – Sabuncu Jan 07 '17 at 02:19
  • 1
    `echo` + _space_ does not allow to return an empty line, you'll receive `ECHO is on|off.` then; `echo.` is a commonly used syntax to avoid that, but this could fail if there is a file called `echo.`; `echo/` avoids that (`/` cannot occur in file names); `echo/` however fails to output a single `?` character (remember the `/?` option); so finally, `echo(` is the only secure way to echo any string... Search [DosTips](http://dostips.com) for an article about safe `echo`... – aschipfl Jan 07 '17 at 15:55
  • @aschipfl Thank you; I read your comment couple of times and finally understand it what's happening. Could you please post it as an answer? Thanks again. – Sabuncu Jan 07 '17 at 16:54

1 Answers1

7

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.

aschipfl
  • 33,626
  • 12
  • 54
  • 99