Windows 命令提示符中的转义尖括号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/251557/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 05:38:45  来源:igfitidea点击:

Escape angle brackets in a Windows command prompt

windows

提问by Jason

I need to echo a string containing angle brackets (< and >) to a file on a Windows machine. Basically what I want to do is the following:
echo some string < with angle > brackets >>myfile.txt

我需要将包含尖括号(< 和 >)的字符串回显到 Windows 机器上的文件中。基本上我想做的是以下内容:
echo some string < with angle > brackets >>myfile.txt

This doesn't work since the command interpreter gets confused with the angle brackets. I could quote the whole string like this:
echo "some string < with angle > brackets" >>myfile.txt

这不起作用,因为命令解释器与尖括号混淆。我可以像这样引用整个字符串:
echo "some string < with angle > brackets" >>myfile.txt

But then I have double quotes in my file that I don't want.

但是后来我的文件中有我不想要的双引号。

Escaping the brackets ala unix doesn't work either:
echo some string \< with angle \> brackets >>myfile.txt

转义括号 ala unix 也不起作用:
echo some string \< with angle \> brackets >>myfile.txt

Ideas?

想法?

回答by Tim Robinson

The Windows escape character is ^, for some reason.

由于某种原因,Windows 转义字符是 ^。

echo some string ^< with angle ^> brackets >>myfile.txt

回答by sin3.14

True, the official escape character is ^, but be careful because sometimes you need three^characters. This is just sometimes:

没错,官方转义字符是^,但要小心,因为有时您需要三个^字符。这只是有时

C:\WINDOWS> echo ^<html^>
<html>

C:\WINDOWS> echo ^<html^> | sort
The syntax of the command is incorrect.

C:\WINDOWS> echo ^^^<html^^^> | sort
<html>

C:\WINDOWS> echo ^^^<html^^^>
^<html^>

One trick out of this nonsense is to use a command other than echoto do the output and quote with double quotes:

这种废话的一个技巧是使用命令而不是echo使用双引号进行输出和引用:

C:\WINDOWS> set/p _="<html>" <nul
<html>
C:\WINDOWS> set/p _="<html>" <nul | sort
<html>

Note that this will not preserve leading spaces on the prompt text.

请注意,这不会保留提示文本上的前导空格。

回答by dbenham

There are methods that avoid ^escape sequences.

有一些方法可以避免^转义序列。

You could use variables with delayed expansion. Below is a small batch script demonstration

您可以使用延迟扩展的变量。下面是一个小批量脚本演示

@echo off
setlocal enableDelayedExpansion
set "line=<html>"
echo !line!

Or you could use a FOR /F loop. From the command line:

或者您可以使用 FOR /F 循环。从命令行:

for /f "delims=" %A in ("<html>") do @echo %~A

Or from a batch script:

或者从批处理脚本:

@echo off
for /f "delims=" %%A in ("<html>") do echo %%~A

The reason these methods work is because both delayed expansion and FOR variable expansion occur after special operators like <, >, &, |, &&, ||are parsed. See How does the Windows Command Interpreter (CMD.EXE) parse scripts?for more info.

这些方法之所以起作用,是因为延迟扩展和 FOR 变量扩展都发生在解析了<, >, &, |, &&,等特殊运算符之后||。请参阅Windows 命令解释器 (CMD.EXE) 如何解析脚本?了解更多信息。



sin3.14 points out that pipes may require multiple escapes. For example:

sin3.14 指出管道可能需要多次转义。例如:

echo ^^^<html^^^>|findstr .

The reason pipes require multiple escapes is because each side of the pipe is executed in a new CMD process, so the line gets parsed multiple times. See Why does delayed expansion fail when inside a piped block of code?for an explanation of many awkward consequences of Window's pipe implementation.

管道需要多次转义的原因是管道的每一侧都在一个新的 CMD 进程中执行,因此该行被多次解析。请参阅为什么在管道代码块中延迟扩展会失败?解释 Window 管道实现的许多尴尬后果。

There is another method to avoid multiple escapes when using pipes. You can explicitly instantiate your own CMD process, and protect the single escape with quotes:

还有另一种方法可以在使用管道时避免多次转义。您可以显式实例化您自己的 CMD 进程,并用引号保护单个转义符:

cmd /c "echo ^<html^>"|findstr .

If you want to use the delayed expansion technique to avoid escapes, then there are even more surprises (You might not be surprised if you are an expert on the design of CMD.EXE, but there is no official MicroSoft documentation that explains this stuff)

如果你想使用延迟扩展技术来避免逃逸,那么还有更多惊喜(如果你是 CMD.EXE 的设计专家,你可能不会感到惊讶,但没有官方的微软文档解释这个东西)

Remember that each side of the pipe gets executed in its own CMD.EXE process, but the process does notinherit the delayed expansion state - it defaults to OFF. So you must explicitly instantiate your own CMD.EXE process and use the /V:ON option to enable delayed expansion.

请记住,管的每一面在自己的CMD.EXE过程被执行,但这一过程不会继承延迟扩张状态-它默认为关闭。因此,您必须显式实例化您自己的 CMD.EXE 进程并使用 /V:ON 选项启用延迟扩展。

@echo off
setlocal disableDelayedExpansion
set "line=<html>"
cmd /v:on /c echo !test!|findstr .

Note that delayed expansion is OFF in the parent batch script.

请注意,延迟扩展在父批处理脚本中处于关闭状态。

But all hell breaks loose if delayed expansion is enabled in the parent script. The following does notwork:

但是,如果在父脚本中启用了延迟扩展,一切都会变得混乱。以下就不能正常工作:

@echo off
setlocal enableDelayedExpansion
set "line=<html>"
REM - the following command fails
cmd /v:on /c echo !test!|findstr .

The problem is that !test!is expanded in the parent script, so the new CMD process is trying to parse unprotected <and >.

问题是,!test!在父脚本扩展,所以新的CMD进程试图解析未受保护的<>

You could escape the !, but that can get tricky, because it depends on whether the !is quoted or not.

您可以转义!,但这会变得棘手,因为这取决于是否!引用了 。

If not quoted, then double escape is required:

如果没有引用,则需要双重转义:

@echo off
setlocal enableDelayedExpansion
set "line=<html>"
cmd /v:on /c echo ^^!test^^!|findstr .

If quoted, then a single escape is used:

如果引用,则使用单个转义符:

@echo off
setlocal enableDelayedExpansion
set "line=<html>"
cmd /v:on /c "echo ^!test^!"|findstr .

But there is a surprising trick that avoids all escapes - enclosing the left side of the pipe prevents the parent script from expanding !test!prematurely:

但是有一个令人惊讶的技巧可以避免所有转义 - 封闭管道的左侧可以防止父脚本!test!过早扩展:

@echo off
setlocal enableDelayedExpansion
set "line=<html>"
(cmd /v:on /c echo !test!)|findstr .

But I suppose even that is not a free lunch, because the batch parser introduces an extra (perhaps unwanted) space at the end when parentheses are used.

但我想即使这也不是免费的午餐,因为当使用括号时,批处理解析器会在末尾引入一个额外的(可能是不需要的)空间。

Aint batch scripting fun ;-)

没有批处理脚本的乐趣;-)

回答by orbitcowboy

In order to use special characters, such as '>' on Windows with echo, you need to place a special escape character before it.

为了在带有 echo 的 Windows 上使用特殊字符,例如 '>',您需要在它之前放置一个特殊的转义字符。

For instance

例如

echo A->B

will no work since '>' has to be escaped by '^':

将不起作用,因为 '>' 必须被 '^' 转义:

 echo A-^>B

See also escape sequences. enter image description here

另请参阅转义序列在此处输入图片说明

There is a short batch file, which prints a basic set of special character and their escape sequences.

有一个简短的批处理文件,它打印一组基本的特殊字符及其转义序列。

回答by James Curran

Escaping the brackets ala unix doesn't work either:

echo some string \< with angle \> brackets >>myfile.txt

转义括号 ala unix 也不起作用:

回显一些字符串 \< 带尖角 \> 括号 >>myfile.txt

The backslash would be considered the start of a absolute pathname.

反斜杠将被视为绝对路径名的开始。

回答by aalaap

You can also use double quotes to escape special characters...

您还可以使用双引号来转义特殊字符...

echo some string "<" with angle ">" brackets >>myfile.txt