Skip to content

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

Open

Description

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 PowerShell/PowerShell#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

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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?

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

    @microsoft-github-policy-service
  7. microsoft-github-policy-service commented on Nov 22, 2023

    @microsoft-github-policy-service
  8. microsoft-github-policy-service commented on Feb 18, 2026

    @microsoft-github-policy-service
  9. daxian-dbw commented on Feb 18, 2026

    @daxian-dbw
    Member

    The ctrl+d behavior in PowerShell Emacs and Vi modes is achieved by 2 functions bound to ctrl+d. They are DeleteCharOrExit and ViAcceptLineOrExit. So, the best solution would be to update those functions to write a new line before ending the process.

    Key    Function         Description
    ---    --------         -----------
    Ctrl+d DeleteCharOrExit Delete the character under the cursor, or if the line is empty, exit the process.
    
    Key      Function           Description
    ---      --------           -----------
    Ctrl+d   ViAcceptLineOrExit If the line is empty, exit, otherwise accept the line as input.
    <Ctrl+d> ViAcceptLineOrExit If the line is empty, exit, otherwise accept the line as input.
    
  10. sdwheeler commented on Feb 18, 2026

    @sdwheeler
    Contributor
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

    Needs-Triage 🔍It's a new issue that core contributor team needs to triage.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions