PowerShell: best way to escape double quotes in string passed to an external program? E.g., a JSON string

前端 未结 1 1981
一向
一向 2020-12-18 11:21

I read I think all the articles on escaping strings in PowerShell, but I still haven\'t found the solution that would satisfy me.

Suppose I have a file foo.jso

相关标签:
1条回答
  • 2020-12-18 11:44

    Unfortunately, PowerShell's passing of arguments with embedded double quotes to external programs is broken, requiring you to manually \-escape them as \":

    myprogram ((Get-Content -Raw ~/foo.json) -replace '"', '\"')
    

    Note the use of Get-Content -Raw, which is preferable to Get-Content ... | Out-String for reading the entire file into a single, multi-line string, but note that it requires PSv3+.

    See this answer for more information.

    0 讨论(0)
提交回复
热议问题