[英]Passing empty strings using double quotes via PowerShell.exe -command
[英]Escaping quotes in powershell.exe via command prompt
看完这篇文章后,我试图转义引号,但是做一个反斜杠不会转义这样的html引号: ”
<-注意,它是一个与普通引号不同的右双引号"
。
例如:
工作正常:
powershell -noexit -command $str = \"hello '123' world\"; write-host $str
不起作用:
powershell -noexit -command $str = \"hello '123'” world\"; write-host $str
我该如何逃避? 或者更好的是我如何一次摆脱所有角色以摆脱这种麻烦!
使用反引号`来转义特殊的双引号,因此:
powershell -noexit -command $str = \"hello '123'” world\"; write-host $str
变成这个:
powershell -noexit -command $str = \"hello '123'`” world\"; write-host $str
编辑:
选项1.如果您更喜欢使用here-strings,则可以将以上内容更改为powershell -file
并运行您的powershell脚本文件。 在此处尽可能多地使用此处字符串。
选项2。如果您不想在调用/处理应用程序/脚本中处理特殊的引号字符,则可以改用单引号。 在这种情况下,您只需将单引号加倍即可转义,如下所示:
powershell -noexit -command $str = 'hello ''123''” world'; write-host $str
请注意,在这里我对您的双引号字符没有执行任何操作,并且一切正常。
因此,您的字符串替换代码可能会变得更易于阅读和维护,尤其是在编辑器无法正确显示此字符的情况下(如Powershell控制台那样-而是显示常规的双引号)。
字符串可通过在文本两边用四引号引起来来识别:“”“”文本“”“”
例:
powershell.exe -command """""Recognized as string """""
Output Stream:> Recognized as string
对于子表达式中的引号,双引号表示8倍的引号
例:
powershell.exe -command """""""""How to quote in subexpression"""""""""
Output Stream:> "How to quote in subexpression"
从命令提示符(实际上是在PowerShell中)运行此命令:
powershell -noexit -command { $str = "hello '123' world"; write-host $str }
产生:
hello '123' world
这会满足您的需求吗?
我遇到过同样的问题,发现使用Powershell 6.x(Core)可以做到这一点:
pwsh -command write-host "`u{22}quoted`u{22}"
输出:
"quoted"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.