请考虑以下PowerShell代码:
@"
"@.GetEnumerator() | %{[int]$_}
在我的电脑上输出
13
10
分别是回车符和换行符的ASCII控制字符的十进制表示。
The same code executed on AppVeyor outputs just a single number:
10
换句话说,PowerShell在系统中使用的字符串之间似乎存在差异。我预计来源为[System.Environment]::newline
,但the same environment AppVeyor environment that output the single character in the here string, output
13
10
代表[System.Environment]::newline
。 [System.Environment]::newline
似乎不是此字符串中换行符的来源。
答案 0 :(得分:1)
PowerShell脚本本质上是一个字符串。并且必须使用"`n"
或"`r`n"
进行行分隔。因此,如果PowerShell字符串文字跨越多行,那么PowerShell解析器会保留在此字符串文字中使用的行分隔符,作为字符串文字值的一部分。
例如:
"'a`nb`r`nc'.GetEnumerator() | %{[int]`$_}" | Set-Content .\Test.ps1
.\Test.ps1
将打印:
97
10
98
13
10
99