question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

PowerShell doesn't print a new line on exit

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:5

github_iconTop GitHub Comments

2reactions
jhoneillcommented, Nov 15, 2021

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.

0reactions
teknoravercommented, Aug 17, 2022

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?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I output text without a newline in PowerShell?
The closes solution seems to use Write-Host with the -NoNewLine parameter. You can not pipe this which is a problem generally, but there...
Read more >
PowerShell Code Breaks: Break Line, Not Code
The best thing is to leave out the line continuation character and simply break at the pipe, the comma, the brace, or the...
Read more >
PowerShell output doesn't show until script ends
Solution: For the line that does not output, try to add " out-host"write-host "test" | out-hostAlso, maybe you want to look into the...
Read more >
Force new lines in output : r/PowerShell
EDIT: I tried removing the n$line** and leaving the **$lines += $resl[$count - $i] + " n" changes in places and I get...
Read more >
wt does not handle newlines properly #4619
But when I tried to run your example it turns out that the back tick does never escape the new line unless it...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found