Skip to content

PowerShell doesn't print a new line on exit #16466

Closed
@teknoraver

Description

@teknoraver

Prerequisites

Steps to reproduce

  1. Start PowerShell on Unix
  2. Press ^D

Expected behavior

mcroce@t490s:~$ pwsh
PowerShell 7.2.0
Copyright (c) Microsoft Corporation.

https://aka.ms/powershell
Type 'help' to get help.

PS /home/mcroce>
mcroce@t490s:~$

Actual behavior

mcroce@t490s:~$ pwsh
PowerShell 7.2.0
Copyright (c) Microsoft Corporation.

https://aka.ms/powershell
Type 'help' to get help.

PS /home/mcroce> mcroce@t490s:~$ 

Error details

No response

Environment data

Name                           Value
----                           -----
PSVersion                      7.2.0
PSEdition                      Core
GitCommitId                    7.2.0
OS                             Linux 5.15.0 #47 SMP Tue Nov 2 23:55:47 CET 2021
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visuals

No response

Activity

added
Needs-TriageThe issue is new and needs to be triaged by a work group.
on Nov 14, 2021
ghost added
In-PRIndicates that a PR is out for the issue
on Nov 14, 2021
jhoneill

jhoneill commented on Nov 15, 2021

@jhoneill

if you are using

 Set-PSReadLineKeyHandler -Chord 'Ctrl+d' -ScriptBlock { [System.Environment]::Exit(0) } 

you can make it

 Set-PSReadLineKeyHandler -Chord 'Ctrl+d' -ScriptBlock { ' ' ;  [System.Environment]::Exit(0) }

to output an empty line firest (there is no need for a space between the quotes, you can put any text 'exiting' or a an empty string, or even $null there.)

Because PSReadline is responsible for handling (and changing or acting on) commands as typed but before they are submitted it won't insert a new line, and if you type EXIT [enter] you already have a new line. Exit is immediate exit and if it suffixed lines to output that would change the results of existing code which uses exit, so it would be a breaking change.

As explained elsewhere #16256 ctrl-D isn't a shortcut key but the end of file marker; different OSes use different characters (ctrl Z gets you out of windows' nslookup, but ctrl D gets you out of the unix/linux version) and not all shells see input as a continuous file stream.

teknoraver

teknoraver commented on Dec 8, 2021

@teknoraver
Author

Hi,

I never argued about ctrl-D. Ctrl-D isn't any shortcut, it's just a way to let the shell send an EOF to the process stdin.
The problem here is that no new line is print after an EOF.

The same issue exists when the shell reads an EOF, it's not related to ctrl-d at all.

mcroce@t490s:~$ echo date |bash
mer 8 dic 2021, 18:05:41, CET
mcroce@t490s:~$ echo date |dash
mer 8 dic 2021, 18:05:44, CET
mcroce@t490s:~$ echo date |zsh
mer 8 dic 2021, 18:05:47, CET
mcroce@t490s:~$ echo date |pwsh
PowerShell 7.2.0
Copyright (c) Microsoft Corporation.

https://aka.ms/powershell
Type 'help' to get help.

PS /home/mcroce> date
mer 8 dic 2021, 18:05:50, CET
PS /home/mcroce> mcroce@t490s:~$

Above you can see that every shell writes a new line when reaching the end of the stdin, but powershell doesn't and clobbers the prompt.
No ctrl-d involved at all this time, just regular end of stream reached.

jhoneill

jhoneill commented on Dec 8, 2021

@jhoneill

I'm not sure if the issue is actually printing the prompt when there is no more input.
You're right at best the behaviour is untidy. I thought this and the referenced query were basically the same thing, my bad on that.

teknoraver

teknoraver commented on Dec 8, 2021

@teknoraver
Author

Well, other shells don't print the prompt if there is an EOF.
I think that this could be solved either by printing a new line, or by suppressing the last prompt.
I looked at the code, the latter is a bit more difficult.

teknoraver

teknoraver commented on Aug 17, 2022

@teknoraver
Author

I just retried with a fresh build, and now ^D doesn't even terminates the shell.
This is the strace log:

mcroce@mcroce-ubuntu18:~$ sudo strace -p 16084
strace: Process 16084 attached
read(0, "\4", 1024)                     = 1
futex(0x7f35c40d28b0, FUTEX_WAKE_PRIVATE, 1) = 1
futex(0x7f35c40d2860, FUTEX_WAKE_PRIVATE, 1) = 1
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_SHARED, 8, 0xac000) = 0x7f76ff696000
munmap(0x7f76ff696000, 4096)            = 0
read(0, "\4", 1024)                     = 1
read(0, "\4", 1024)                     = 1
read(0, "\4", 1024)                     = 1
read(0, 

What has been changed?

microsoft-github-policy-service

microsoft-github-policy-service commented on Nov 15, 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.

microsoft-github-policy-service

microsoft-github-policy-service commented on Nov 22, 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.

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

    In-PRIndicates that a PR is out for the issueNeeds-TriageThe issue is new and needs to be triaged by a work group.Resolution-No ActivityIssue has had no activity for 6 months or moreWG-Interactive-Consolethe console experience

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      PowerShell doesn't print a new line on exit · Issue #16466 · PowerShell/PowerShell