如何在Powershell控制台中分配多行字符串

问题描述 投票:0回答:3

当我在 powershell 控制台中输入此内容时

$test=@'
Test
Test'@

并且输入多次,它会一直打印

>>

所以我永远无法完成命令。

该怎么办?

string powershell multiline
3个回答
116
投票

@'
应该是该行中的第一件事,否则它被认为只是字符串的一部分。

$test=@'
Test
Test
'@

此方法也适用于

@"
/
"@


27
投票
$test=@'
Test
Test
'@

需要注意的重要一点是分隔符包括(不可见的)回车符。起始标签末尾必须有一个,结束标签之前必须有一个。


20
投票

根据 PowerShell 最佳实践和风格指南中关于最大行长度的部分,我建议“splatting”字符串,如下所示:

$myStr = ("The family of Dashwood had long been settled in Sussex. Their estate was " +
              "large, and their residence was at Norland Park, in the centre of their " +
              "property, where, for many generations, they had lived in so respectable " +
              "a manner as to engage the general good opinion of their surrounding " +
              "acquaintance.")
© www.soinside.com 2019 - 2024. All rights reserved.