Skip to content

(some) command read from stdin seem to require extra trailing newline #21644

Open
@philcerf

Description

@philcerf

Prerequisites

Steps to reproduce

I see the following in current PowerShell versions back to the days of powershell.exe.

Having the following files:

$ xxd lf.ps1 
00000000: 4765 742d 5072 6f63 6573 7320 7c0a 5768  Get-Process |.Wh
00000010: 6572 652d 4f62 6a65 6374 207b 245f 2e4e  ere-Object {$_.N
00000020: 616d 6520 2d49 4c69 6b65 2027 2a65 7870  ame -ILike '*exp
00000030: 6c6f 7265 722a 277d 0a                   lorer*'}.
$ xxd crlf.ps1 
00000000: 4765 742d 5072 6f63 6573 7320 7c0d 0a57  Get-Process |..W
00000010: 6865 7265 2d4f 626a 6563 7420 7b24 5f2e  here-Object {$_.
00000020: 4e61 6d65 202d 494c 696b 6520 272a 6578  Name -ILike '*ex
00000030: 706c 6f72 6572 2a27 7d0d 0a              plorer*'}..

(i.e. happens with both, LF and CRLF newlines)

and executing them via -File works as expected:

$ pwsh -File lf.ps1 

 NPM(K)    PM(M)      WS(M)     CPU(s)      Id  SI ProcessName
 ------    -----      -----     ------      --  -- -----------
     73    41,08     108,07       8,95    3772   4 explorer

$ pwsh -File crlf.ps1 

 NPM(K)    PM(M)      WS(M)     CPU(s)      Id  SI ProcessName
 ------    -----      -----     ------      --  -- -----------
     73    41,08     108,07       8,95    3772   4 explorer

But If I do the same from stdin:

$ pwsh -Command - <lf.ps1 
$ pwsh -Command - <crlf.ps1 

there's no output at all and the exit code is still 0.

It does suddenly work, if I add another trailing newline (either LF or CRLF) at the end of the file.

What also works is:

$ pwsh -Command "Get-Process | 
Where-Object {\$_.Name -ILike '*explorer*'}"

This is executed from bash, and the string passed as argv to -Command contains a LF newline, i.e. binary it looks like:

$ printf '%s' "Get-Process | 
Where-Object {\$_.Name -ILike '*explorer*'}" | xxd 
00000000: 4765 742d 5072 6f63 6573 7320 7c20 0a57  Get-Process | .W
00000010: 6865 7265 2d4f 626a 6563 7420 7b24 5f2e  here-Object {$_.
00000020: 4e61 6d65 202d 494c 696b 6520 272a 6578  Name -ILike '*ex
00000030: 706c 6f72 6572 2a27 7d                   plorer*'}

So if the command is given as string, it works even without terminating newline for the last line.

Any ideas why it doesn't work when reading the command from stdin?

btw: I do the this on Windows and invoking Cygwin.

Thanks,
Philippe

Expected behavior

Should execute the command.

Actual behavior

Doesn't execute the command, or at least produces no output.

Error details

No response

Environment data

$ echo "$PSVersionTable" | pwsh -Command -

gives no output either ;-)



### Visuals

_No response_

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs-TriageThe issue is new and needs to be triaged by a work group.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions