有没有办法在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

我在网上看了一下,它说要逃避$,您必须使用反引号。似乎对变量不起作用吗?

etric520 回答:有没有办法在Powershell中转义变量调用?

也许使用以下语法:

$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
本文链接:https://www.f2er.com/3102041.html

大家都在问