The behavior of line breaks in the Powershell here String in different versions

Source: Internet
Author: User
Tags http post

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.