All*_*ack 3 string powershell naming naming-conventions
这个名字背后的原因是什么?
SS64在PowerShell中解释了这里的字符串如下:
此处字符串是单引号或双引号字符串,可以跨越多行.不评估单引号字符串中的表达式.
here-string中的所有行都被解释为字符串,即使它们没有用引号括起来.
Run Code Online (Sandbox Code Playgroud)$myHereString = @' some text with "quotes" and variable names $printthis some more text '@
bri*_*ist 13
它们在PowerShell中具有此名称,因为它是从Unix样式的shell中借用的,就像PowerShell中的许多其他元素和概念一样:
这里的文档源自Unix shell,可以在sh,csh,ksh,bash和zsh等中找到.
至于为什么最初使用该名称,该文章并不严格涵盖词源,但基于此描述:
在计算中,here文档(here-document,here-text,heredoc,hereis,here-string或here-script)是文件文字或输入流文字:它是源代码文件的一部分,被视为它是一个单独的文件.该术语还用于使用类似语法的多行字符串文字形式,在文本中保留换行符和其他空格(包括缩进).
似乎合乎逻辑的是,名称的"此处"部分指的是"此处"包含的文件(如此处所示).
为了完整起见,值得注意的是PowerShell还直接在单引号和双引号中支持换行[string]
,如下所示:
$myString = 'some text with "quotes" and variable names $printthis
some more text'
Run Code Online (Sandbox Code Playgroud)
here-string有用的一个更好的例子是当你需要两种类型的引号时:
$myHereString = @'
some text with "quotes" and variable names $printthis
some more text
and my grandma's soup
'@
Run Code Online (Sandbox Code Playgroud)
如果这只是一个单引号字符,则'
在grandma's
需要进行转义.
如果它是一个双引号字符串,你需要躲避的情况下,"
和你需要逃避$
,如果你不希望变量扩展.