CMD中单引号和双引号之间的差异 [英] Differences between single and double quotes in CMD
问题描述
我有一个Powershell文件,它替换了配置文件中的IP. Powershell包含三个参数:
I have a powershell file that replaces an IP in a config file. The powershell takes in three parameters:
- 文件名(
'C:\Program Files (x86)\app\app.conf'
) - 要替换的IP
- 新IP
由于我们依赖于为服务使用批处理文件来处理任务,因此我编写了一个批处理脚本,该脚本只接受三个参数并将其发布到powershell脚本中.
Since we are dependent on using a batch file for our service to process the task, I wrote a batch script that simply takes three parameters and posts that to the powershell script.
我们使用批处理文件来调用和解析参数: ($仅用于表示变量)
We use batch file for calling and parsing the parameter: ($ just used to represent variables)
changeip.bat '$filepath' $oldip $newip
这失败了,但是我不明白为什么.我试过在$filepath
周围使用双引号而不是单引号,并且有效.
This fails, but I don't understand why. I've tried using double quotes instead of single quotes around $filepath
and it worked.
如何用双引号而不是单引号理解$filepath
中的空格?
How can the whitespace in $filepath
be understood by double quotes and not single quotes?
推荐答案
简短的答案是 CMD不会将单引号当作常规字符来对待.它不是像成对的/成组的双引号.
The short answer is that CMD doesn't treat single quotes as anything but a regular character. It's not a pair/group like double quotes.
单引号具有任何特殊含义的唯一位置是在FOR /F
循环中,在该循环中您指定要运行的命令并遍历输出.
The only place where the single quote has any special meaning is in a FOR /F
loop, where you are specifying a command to run and iterate over the output.
FOR /F %%d IN ('DIR /S /B C:\Windows') DO @ECHO File: %%d
FOR /F
可以选择使用反引号代替,以防您需要将单引号传递给被调用的进程,但这与您的问题无关.
FOR /F
has an option to use backticks instead, in case you need to pass single quotes to the called process, but this is unrelated to your question.
这篇关于CMD中单引号和双引号之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!