通过命令提示符转义powershell.exe中的引号

Mor*_*834 9 powershell cmd

在查看这篇文章之后,我试图逃避引号,但做反斜杠不会逃避这样的html引号:< - 注意它是一个正确的双引号不同于正常".

例如:

工作良好:

powershell -noexit -command $str = \"hello '123' world\"; write-host $str
Run Code Online (Sandbox Code Playgroud)

不起作用:

powershell -noexit -command $str = \"hello '123'” world\"; write-host $str
Run Code Online (Sandbox Code Playgroud)

我该怎么逃避这个?或者更好的我如何一次性逃脱所有角色摆脱这个麻烦!

Neo*_*isk 10

使用反引号`来逃避您的特殊双引号,所以这:

powershell -noexit -command $str = \"hello '123'” world\"; write-host $str
Run Code Online (Sandbox Code Playgroud)

成为这个:

powershell -noexit -command $str = \"hello '123'`” world\"; write-host $str
Run Code Online (Sandbox Code Playgroud)

编辑:

选项1.如果您更喜欢使用here-strings,则可以将上述内容更改为powershell -file并运行您的powershell脚本文件.尽可能多地使用here-strings.

选项2.如果您不想在调用/处理应用程序/脚本中处理特殊引号字符,则可以切换到使用单引号.在这种情况下,你只需要通过将它们加倍来逃避单引号,如下所示:

powershell -noexit -command $str = 'hello ''123''” world'; write-host $str
Run Code Online (Sandbox Code Playgroud)

请注意,我没有对你的双引号字符做任何事情,它工作得很好.

所以你的字符串替换代码可能会变得稍微容易阅读和维护,特别是如果编辑器没有正确显示这个字符(就像Powershell控制台那样 - 它显示了常规的双引号).


小智 5

  1. 用“”启动命令开关
  2. 字符串是通过两边用 4 次引号括起来的文本来识别的: """" text """"

    例子:

    powershell.exe -command """""Recognized as string """""
    
    Run Code Online (Sandbox Code Playgroud)
    Output Stream:> Recognized as string
    
    Run Code Online (Sandbox Code Playgroud)
  3. 对于子表达式中的引号,双引号,即 8 倍引号

    例子:

    powershell.exe -command """""""""How to quote in subexpression"""""""""
    
    Run Code Online (Sandbox Code Playgroud)
    Output Stream:> "How to quote in subexpression"
    
    Run Code Online (Sandbox Code Playgroud)

  • 哇!8 个双引号,即 16个单人。命令提示符和 Powershell 是完美的搭配;) (7认同)

Rob*_*bin 2

从命令提示符运行此命令(实际上是在 PowerShell 中):

powershell -noexit -command { $str = "hello '123' world"; write-host $str }
Run Code Online (Sandbox Code Playgroud)

生成:

hello '123' world
Run Code Online (Sandbox Code Playgroud)

这能满足您的需要吗?

  • 这并没有解决 OP 问题中的智能引号字符。是的,在某些屏幕上很难将该字符与普通双引号区分开来。在我的 Surface 2 上确实很清楚,但在我的桌面上看这个,很难在问题中使用的比例字体中看到。 (2认同)