在 Windows CMD 文件中,后跟斜杠的 echo 有什么作用? [英] What does an echo followed immediately by a slash do in a Windows CMD file?
问题描述
检查此 Windows 批处理/CMD 文件时 (RefreshEnv.cmd 来自 Chocolatey 源),我遇到了以下代码行:
echo/@echo off >"%TEMP%\_env.cmd"
以上创建文件 _env.cmd 内容为@echo off".
echo/"中的斜线有什么作用?如果我省略斜线,则生成的文件完全相同.
我使用的是 Windows 7.
首先,对于你的命令行,无论你是写echo/@echo off
还是echo @echo off
,绝对没有区别,只有文字@echo off
回显.
紧跟在 echo
命令之后的 /
字符有时用于回显空行,因为没有任何参数的 echo
返回类似这样的消息ECHO 开启|关闭.
.一种非常常见的方法是编写 echo.
,尽管如果当前工作中存在名为 echo.
(无文件扩展名)的文件,这可能会失败目录,然后会无意中执行.
一般来说,cmd
中有以下字符用来分隔命令行上的标记,例如命令及其参数:SPACE、TAB、、
、;
、=
和非中断空间(代码 0xFF
).最常用的标记分隔符(我强烈推荐)是 SPACE.多个相邻的分隔符被视为一个.
还有一些字符似乎将命令与其参数分开:.
、:
、/
、、
[
、]
、+
和 (
,虽然这些字符似乎成为第一个的一部分然后参数.(尝试type+file.txt
输入+file.txt
的内容,而不是file.txt
的内容.)><小时>然而,
echo
似乎表现不同,因为并非所有标记分隔符都得到同等处理,并且上述将命令与其参数分开的附加字符不被视为参数的一部分.这是 echo
的行为方式:
echo
后跟无,或后跟 SPACE、TAB 或不间断空格,或由多个组成的字符串此类字符返回ECHO is on|off.
;echo
后跟、
、;
或=
返回空行;echo
后跟一个、
、一个;
或一个=
,后跟一个或多个标记分隔符返回这些标记分隔符;echo
后跟一个标记分隔符,后跟一个字符串,其中至少包含一个除 SPACE、TAB 和非中断空格以外的字符返回那个字符串;echo
后跟.
,:
,/
,,
[
,]
,+
或(
返回空行;echo
后跟.
,:
,/
,,
[
,]
,+
或(
,后跟其他任何内容,甚至仅标记分隔符,返回所说的任何内容;echo
后跟.
、[
、]
或+
失败,如果有一个名为echo.
、echo[
、echo]
和echo+
的文件;echo
后跟或
:
也可能由于类似(但更复杂)的路径冲突而失败;echo
后跟/
不会因为路径冲突而失败,但是如果后面的文字以?
开头就会失败,因为/?
被注意到,所以在控制台中显示帮助文本而不是给定的文本;echo
后跟(
,后跟任意文本安全地返回所述任意文本;
因此,回显任意文本的唯一安全方法是使用 echo(
.对于返回空行,我更喜欢使用 echo/
,因为它看起来不像那个奇怪的 echo(
.
DosTips,了解如何通过 echo
命令安全地返回空行以及如何安全地使用 echo
命令.
注意^
、&
、%
、!
、)等字符
、<
、>
和 |
需要正确转义才能被 echo
正确返回;然而,这是必要的,因为命令解释器 cmd
与 echo
命令无关.
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.
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, ,
, ;
, =
and the non-break space (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 or a non-break space, or a string composed by multiple such characters returnsECHO 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 and non-break space 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 namedecho.
,echo[
,echo]
andecho+
, respectively;echo
followed byor
:
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.
这篇关于在 Windows CMD 文件中,后跟斜杠的 echo 有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!