Description
Encounters a hole in a PowerShell here string resulting in a break, here to verify the behavior of the PowerShell here string under different versions. If you don't mind, you're likely to step on the pit.
Here string representation in the PS1
- You cannot use the empty here string, or you will find that you have typed multiple returns in the console, and this here string cannot be closed.
- If there is a newline in here string, the line feed is eaten.
Here string representation in the PS2
- You can use the empty here string
- If there is a newline in here string, the line feed is eaten.
Here string representation in the PS3
Same as PS2, verification method with PS2
Here string representation in the PS4
Same as PS2, verification method with PS2
Here string representation in the PS5
- You can use the empty here string
- If there is a newline in the here string, the line break is preserved.
- The newline character is \ n, not \ r \ n-CRLF, which can be very significant in certain cases (such as HTTP Post data newline characters are explicitly required to be CRLF, less a \ r, you may Post will be wrong.) )
Summarize
- If you want to keep line breaks in PowerShell here string, the way you display them is written directly below
[email protected]"`r`naa"
But you will find that PowerShell will add one more to you in multiple versions \ n
PS C:\> [email protected]">> `r`n>> aa>> "@>>PS C:\> $a.length5
- The ultimate solution, an explicit uniform line break for a certain type of
PS C:\> [email protected]">> `r`n>> aa>> "@>>PS C:\> $a.legnthPS C:\> $a.length5PS C:\> $b=$a -replace ‘(\r\n|\r|\n)‘,"`r`n"PS C:\> $b.length6PS C:\> $baaPS C:\>
The behavior of line breaks in the Powershell here String in different versions