在Windows CMD文件中,回声后紧跟斜杠是什么意思? [英] What does an echo followed immediately by a slash do in a Windows CMD file?

查看:68
本文介绍了在Windows CMD文件中,回声后紧跟斜杠是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在检查此Windows批处理/CMD文件时( RefreshEnv. cmd (来自Chocolatey来源),我遇到了以下代码行:

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"

上面创建的文件 _env.cmd 的内容为"@echo off".

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

在"echo/"中,斜杠是做什么的?如果我省略斜杠,则生成的文件完全相同.

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

我在Windows 7上.

I am on Windows 7.

推荐答案

首先,对于您的命令行,无论您写的是echo/@echo off还是echo @echo off,都没有任何区别,只是回显了文本@echo off.

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.

echo命令之后的/字符有时用于回显空行,因为echo不带任何参数会返回类似ECHO is on|off.的消息.一种非常普遍的方法是编写echo.,尽管如果当前工作目录中存在一个名为echo.(没有文件扩展名)的文件,则此操作可能会失败,然后将无意执行该文件.

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.

通常,cmd中有以下字符在命令行上分隔标记,例如命令及其参数: SPACE TAB ;=和不间断空格(代码0xFF).最常用的令牌分隔符(强烈建议)是 SPACE .多个相邻的分隔符被视为一个分隔符.

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.

似乎还有更多字符可以将命令与其参数分开:.:/\[]+(,尽管这些字符似乎成为了第一个论点的一部分. (尝试type+file.txt键入+file.txt的内容,但不能键入file.txt.)

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的行为似乎有所不同,因为并非所有标记分隔符都得到同等处理,并且上述将命令与其命令参数分开的附加字符并未被视为参数的一部分. echo的行为是这样的:

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,后面没有任何内容,或者后面是 SPACE TAB 或不间断空格,或者由多个此类字符组成的字符串返回;
  • echo后跟,;=返回空行;
  • echo,后跟,;=,后跟一个或多个令牌分隔符,将返回这些令牌分隔符;
  • echo,后跟一个令牌分隔符,后跟一个包含至少一个字符的字符串,该字符串至少包含一个 SPACE TAB 和不间断空格以外的字符,返回该字符串;
  • echo后跟.:/\[]+(会返回空行;
  • echo,后跟.:/\[]+(,后面紧跟其他内容,甚至仅是令牌分隔符,返回所说的任何内容;
  • 如果存在分别名为echo.echo[echo]echo+的文件,则
  • echo后跟.[]+失败;
  • echo后跟\:的方法也可能由于类似(但更为复杂)的路径冲突而失败;
  • echo跟在/之后的内容不会因路径冲突而失败,但是如果以下文本以?开头,则会失败,因为会注意到/?,因此帮助文本将显示在控制台中,而不是给定的文字;
  • echo后跟(,后跟任意文本可安全返回所述任意文本;
  • echo followed by nothing, or followed by a SPACE, a TAB or a non-break space, 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 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 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;

因此,回显任何任意文本的唯一安全方法是使用echo(.对于返回空行,我更喜欢使用echo/,因为它看起来不像echo(那样奇怪.

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.

关于^&%!)<>|之类的字符需要正确转义才能由;但是,由于命令解释器cmd这是必需的,而与echo命令无关.

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文件中,回声后紧跟斜杠是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆