简体   繁体   English

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

[英]How to assign multiple lines string in Powershell Console

When I do enter this in powershell Console当我在 powershell 控制台中输入此内容时

$test=@'
Test
Test'@

And do enter several times, it keeps printing并多次输入,它一直在打印

>>

So I can never finish command.所以我永远无法完成命令。

What to do ?该怎么办 ?

'@ should be first thing in the line or it is considered to be just a part of the string. '@应该是行中的第一件事,或者它被认为只是字符串的一部分。

$test=@'
Test
Test
'@

This approach also works with @" / "@这种方法也适用于@" / "@

As per the section on maximum line length in The PowerShell Best Practices and Style Guide , I would suggest “splatting” the string, like this:根据The PowerShell Best Practices and Style Guide 中关于最大行长度的部分,我建议“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.")
$test=@'
Test
Test'@

The important thing to note is that the delimiters include (invisible) carriage returns.需要注意的重要一点是分隔符包括(不可见的)回车符。 There must be one at the end of the starting tag, and one before the closing tag.必须有一个在起始标签的末尾,一个在结束标签之前。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM