有没有办法在 powershell 中转义变量调用?

Is there a way to escape a variable call in powershell?

我正在尝试使用 powershell 为 visual studio 生成 build.include 脚本。他们都使用 ${variable} 作为引用变量的方式。

我想知道是否有办法在字符串中转义它。

例如

$string = @'
This is a `${string} I want to escape and {0} one I do not want to
'@ -f $Var

我在网上看了一下,说要转义 $ 你必须使用反引号。不过似乎对变量不起作用?

可以使用以下语法:

$Var = 'one'

$string = @"
This is a `$Var I want to escape and {0} I do not want to
"@ -f $Var

$string

生成以下输出:

This is a $Var I want to escape and one I do not want to

另一个选项是嵌入变量:

@"
This is a `${Var} I want to escape and $Var I do not want to
"@

一些备注:

  • 这里的字符串总是在双引号之间@""@
  • 要转义字符,请使用反引号`

我明白了。你必须放双括号。

示例:

$string = @'
This is a ${{string}} I want to escape and {0} one I do not want to
'@ -f $Var