powershell格式为字符串
我有一个要动态插入变量的字符串。例子;
$tag = '{"number" = "5", "application" = "test","color" = "blue", "class" = "Java"}'
我想完成:
$mynumber= 2
$tag = '{"number" = "$($mynumber)", "application" = "test","color" = "blue", "class" = "Java"}'
我想在字符串上插入变量,,但它没有通过。我猜“”将全部设置为字符串。关于我该如何处理的任何建议?
Powershell测试以及反复试验。还有Google。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要添加到 Mathias'有用的答案:
错误地期望在
'...'...'...'...'字符串(而不是内部
“ ...”
)以前出现了很多次,诸如您的问题通常会以此帖子。但是,您的问题值得单独回答,因为:
您的用例引入了后续问题,即嵌入式
“
字符在“ ...”中无法用作字符。
。更一般地,链接的帖子是在 crign-passing 的上下文中,其中其他规则适用。
注意:下面的一些链接与概念 about_quoting_rules 帮助主题。
在PowerShell中:
唯一
“ ...”
strings (双引号,称为 可扩展字符串 ) 强>,即变量值的扩展(例如“ ... $ var”
和subexpressions(例如,“ ... $($ var.prop)”
)< /p>不
'...'
strings(单引号,称为 verbatim strings ),其值使用 verbatim (实际上是)。<<<<<<<<<<<<<<<<<<<<<<<<<< /p>使用
“ ...”
,如果字符串值本身包含”
chars。:要么 将它们作为
`“
或”逃脱了
,例如,使用
),对于独立变量引用,例如`“
;请注意,当使用$(...)
时,“ noreferrer”> subexpression operationer 从不伤害mynumber)$ mynumber
:是不需要的。
同样,如果您想要有选择地 uspress string interpolation ,susce
$
as`$ <$ < /代码>
请参阅概念帮助主题以获取有关逃脱和逃脱序列的信息。
,使用如果您需要 embed
'...'...''
内部'''
,或使用(单引)此处的弦乐(请参阅下一个)。或 使用双引号 there-string 而不是(
@“
@”&lt; newline&gt; ...&gt; ...&lt; newline&gt;“@@
):@“
/@'
)“@
/'@
)必须在该行的开始时 - 甚至甚至没有之前都可能出现它。相关答案:
powerShell's 可扩展字符串
概述
的概述 字符串文字的形式
将字符串作为命令参数 时,它们在情境上是隐式被视为可扩展的字符串(即它们是
“ ...”
添加) ;例如写入输出$ home \ projects
- 请参阅此答案。。替代方案字符串插值:
在情况下,动态构造字符串的其他方法可能很有用:
使用(verbatim) template string 占位符,带有
-f
,格式运算符:/em>使用
+
操作员:To add to Mathias' helpful answer:
Mistakenly expecting string interpolation inside
'...'
strings (as opposed to inside"..."
) has come up many times before, and questions such as yours are often closed as a duplicate of this post.However, your question is worth answering separately, because:
Your use case introduces a follow-up problem, namely that embedded
"
characters cannot be used as-is inside"..."
.More generally, the linked post is in the context of argument-passing, where additional rules apply.
Note: Some links below are to the relevant sections of the conceptual about_Quoting_Rules help topic.
In PowerShell:
only
"..."
strings (double-quoted, called expandable strings) perform string interpolation, i.e. expansion of variable values (e.g."... $var"
and subexpressions (e.g.,"... $($var.Prop)"
)not
'...'
strings (single-quoted, called verbatim strings), whose values are used verbatim (literally).With
"..."
, if the string value itself contains"
chars.:either escape them as
`"
or""
E.g., with
`"
; note that while use of$(...)
, the subexpression operator never hurts (e.g.$($mynumber)
), it isn't necessary with stand-alone variable references such as$mynumber
:Similarly, if you want to selectively suppress string interpolation, escape
$
as`$
See the conceptual about_Special_Characters help topic for info on escaping and escape sequences.
If you need to embed
'
inside'...'
, use''
, or use a (single-quoted) here-string (see next).or use a double-quoted here-string instead (
@"<newline>...<newline>"@
):@"
/@'
)"@
/'@
) must be at the very start of the line - not even whitespace may come before it.Related answers:
Overview of PowerShell's expandable strings
Overview of all forms of string literals in PowerShell
When passing strings as command arguments, they are situationally implicitly treated like expandable strings (i.e. as if they were
"..."
-enclosed); e.g.Write-Output $HOME\projects
- see this answer.Alternatives to string interpolation:
Situationally, other approaches to constructing a string dynamically can be useful:
Use a (verbatim) template string with placeholders, with
-f
, the format operator:Use simple string concatenation with the
+
operator:您当前尝试不起作用的原因是,PowerShell中的单引号(
'
)字符串是 verbatim strings - 不尝试扩展子表达管道或变量表达。如果您想要一个可扩展的字符串文字而无需逃脱字符串本身中包含的所有双引号(
“
),请使用此处的字符串:The reason your current attempt doesn't work is that single-quoted (
'
) string literals in PowerShell are verbatim strings - no attempt will be made at expanding subexpression pipelines or variable expressions.If you want an expandable string literal without having to escape all the double-quotes (
"
) contained in the string itself, use a here-string: