簡體   English   中英

通過命令提示符在powershell.exe中轉義引號

[英]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控制台那樣-而是顯示常規的雙引號)。

  1. 使用“”啟動命令開關
  2. 字符串可通過在文本兩邊用四引號引起來來識別:“”“”文本“”“”

    例:

     powershell.exe -command """""Recognized as string """"" 
     Output Stream:> Recognized as string 
  3. 對於子表達式中的引號,雙引號表示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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM