Skip to content

PowerShell's piping of strings with implicit trailing CRLF newlines causes problems with WSL on Windows #13579

Closed

Description

  • On Windows, PowerShell terminates strings sent via the pipeline to native programs with an implicit trailing CRLF.

  • If WSL (wsl.exe) happens to be targeted, the ultimately invoked Unix utility usually only expects LF newlines, retaining the CR instances as part of the data, causing problems, as shown in the awk example below.

Note:

  • I'm not sure where the proper place to resolve this is. Arguably, it is wsl.exe (and bash.exe) that should translate CRLF sequences to LF sequences before processing stdin input, but that should only be done for text input, and it is only the calling shell that knows whether data is textual or not (if PowerShell is the calling shell, the input is always text, but that's not necessarily always true).

  • A simple way to solve the problem is to make PowerShell always send stdin input with LF-only newlines ("`n"), under the assumption that even native Windows programs recognize such newline sequences; while technically a breaking change, it may be a Bucket 3: Unlikely Grey Area change, and therefore acceptable.

Steps to reproduce

On Windows, with WSL and an Ubuntu distro as the default:

The following should output two one - a single line.

'one two' | wsl -e awk '{ print $2, $1 }' | should -be 'two one'

Expected behavior

The test should pass.

Actual behavior

Expected 'two one', but got @('two', ' one').

What the output doesn't directly show is the root cause of the problem: two is actually two`r (verbatim two followed by a CR in the raw output, which awk retained as part of field $2 due to not considering CRs part of newlines), due to the input line using CRLF as the trailing newline.

Since PowerShell recognizes a CR by itself as a newline too, the output is parsed as two lines.

Environment data

PowerShell Core 7.1.0-preview.6

Activity

  1. added
    Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get a
    on Sep 4, 2020
  2. changed the title [-]PowerShell's piping of strings with CRLF newlines causes problems with WSL[/-] [+]PowerShell's piping of strings with implicit trailing CRLF newlines causes problems with WSL on Windows[/+] on Sep 4, 2020
  3. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  4. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  5. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  6. microsoft-github-policy-service commented on Nov 23, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

  7. Universal-Invariant commented on Jan 31, 2026

    @Universal-Invariant

    This also seems to cause problems with inline code such as:

    wsl -u root -e bash -c @"
      set -e  
      mkdir -p /tmp/test
      mkdir -p /tmp/test2
    "@
    

    It seems that the end of lines in windows are causing invalid chars in the arguments passed to the commands. Putting all the commands on the same line works.

    wsl -u root -e bash -c "set -e; mkdir -p /tmp/test;   mkdir -p /tmp/test2"
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aOS-WSLResolution-No ActivityIssue has had no activity for 6 months or moreWG-Enginecore PowerShell engine, interpreter, and runtime

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions