Windows批处理文件中的单引号有什么作用? [英] What does single-quoting do in Windows batch files?

查看:466
本文介绍了Windows批处理文件中的单引号有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近在Windows cmd中运行了一个带有单引号的命令,发现它的行为不符合我的预期.

I recently ran a command in windows cmd with single quotes in it and found that it did not behave as I expected.

从尝试Google到"Windows批处理单引号与双引号"之间,我发现了几篇文章,人们在其中询问有关Windows将单引号视为双引号,以及bash如何区别对待单引号和双引号的问题.但是,我无法轻易找到Windows批处理命令中单引号的确切解释.

From trying to google "windows batch single quote vs double quote" I've found a few articles where people asked about having windows treat single quotes like double quotes, and about how bash treats single quotes and double quotes differently. However, I couldn't easily find an explicit explanation of what single quotes do in windows batch commands.

那么Windows批处理文件中的单引号是做什么的?他们是否完全执行任何形式的报价功能?

So what do single quotes do in windows batch files? Do they perform any sort of quoting functionality at all?

推荐答案

cmd.exe命令处理器根本不使用单引号,只是为了将命令包含在FOR/F语句中而已:

Single quotes are not used at all by the cmd.exe command processor except to enclose the command to run within a FOR /F statement:

for /f %%A in ('someCommand') do ...

或者如果使用USEBACKQ选项,则指定要由FOR/F处理的字符串:

Or to specify a string to process by FOR /F if the USEBACKQ option is used:

for /f "usebackq" %%A in ('some string') do ...

cmd.exe处理的所有其他引用均用双引号引起来.但是,有些Windows实用程序(外部命令)(例如WMIC)将单引号用于自己的引号目的.但是没有标准-每个实用程序都可以对单引号有自己的解释.

All other quoting handled by cmd.exe is done with double quotes. However, there are Windows utilities (external commands) like WMIC that use single quotes for their own quoting purposes. But there is no standard - each utility is free to have its own interpretation of single quotes.

cmd.exe与各种命令行实用程序所使用的转义和引用规则不一致,这常常使查找任何给定情况所需的正确语法变得困难.

The inconsistent escape and quote rules used by cmd.exe vs. various command line utilities often makes it a challenge to discover the correct syntax needed for any given situation.

有关在cmd.exe中使用双引号"的一些重要说明.

Here are some important notes about using double quotes " with cmd.exe.

  • 双引号是一个简单的状态机.第一个"打开引用,第二个关闭它.

  • Double quotes are a simple state machine. The first " turns quoting on, the next turns it off.

所有特殊字符在加引号时均被视为文字值,始终用于%<newLine>字符,而!字符(如果启用了延迟扩展),以及^字符(如果启用了延迟扩展),并且!也在行内的某处显示.

All special characters are treated as literal values when quoted, except for the % and <newLine> characters always, and the ! character if delayed expansion is enabled, and the ^ character if delayed expansion is enabled and ! also appears somewhere within the line.

如果引用关闭,则可以将"转换为^"以防止其打开引用.但是,一旦打开引号,就无法转义结束的".接下来的"将始终关闭引号.

If quoting is off, then you can escape a " as ^" to prevent it from turning quoting on. But once quoting is on, you cannot escape the closing ". The very next " will always turn quoting off.

这篇关于Windows批处理文件中的单引号有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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