通过命令提示符转义powershell.exe中的引号 [英] Escaping quotes in powershell.exe via command prompt
问题描述
我试图转义引号,但是做一个反斜杠不能转义html引号,像这样: - 注意它是一个正确的双引号不像正常。
I am trying to escape quotes, but doing a backslash does not escape html quotes like this: " <-- notice it's a right double quote unlike the normal ".
例如:
工作正常:
powershell -noexit -command $ str = \hello'123'世界\;写主机$ str
E.g: Works fine: powershell -noexit -command $str = \"hello '123' world\"; write-host $str
无效:
powershell -noexit -command $ str = \ hello'123'world\; write-host $ str
Does not work: powershell -noexit -command $str = \"hello '123'" world\"; write-host $str
如何逃避这个?
推荐答案
使用反引号`来转义特殊的双引号,
Use a backtick ` to escape your special double quote, so this:
powershell -noexit -command $str = \"hello '123'" world\"; write-host $str
变为:
powershell -noexit -command $str = \"hello '123'`" world\"; write-host $str
EDIT:
选项1。如果您更喜欢使用here-strings,可以将以上内容更改为 powershell -file
您的powershell脚本文件。
Option 1. If you prefer using here-strings, you can change the above to powershell -file
and run your powershell script file. Use here-strings as much as you want there.
选项2。如果您不想处理您的特殊引号字符,处理应用程序/脚本,您可以切换到使用单引号。在这种情况下,你只需要转义你的单引号,通过加倍它们,像这样:
Option 2. If you prefer not to deal with your special quote character inside calling/processing application/script, you can switch to using single quotes instead. In this case you only need to escape your single quotes, by doubling them, like this:
powershell -noexit -command $str = 'hello ''123''" world'; write-host $str
注意这里我没有对你的双引号字符做任何事,
Notice here I did not do anything to your double quote character, and it works just fine.
所以你的字符串替换代码可能会变得稍微容易阅读和维护,特别是如果编辑器没有正确显示这个字符(像Powershell控制台 - 一个常规的双引号)。
So your string replace code may become slightly easier to read and maintain, especially if the editor does not display this character correctly (like Powershell console does - it shows a regular double quote instead).
这篇关于通过命令提示符转义powershell.exe中的引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!