New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Out-String should not append a trailing newline #14444
Comments
|
|
I don't think it should. Why do you think so? I would expect: Get-Dateand Get-Date | Out-Stringto produce the same output, yet they don't (the latter has an extra newline). Given that piping to In other words: PS> $out = cmd /c 'echo a&echo b' | Out-Stringshould be equivalent to: PS> $out = (cmd /c 'echo a&echo b') -join [Environment]::NewLineyet it isn't, due to the trailing newline. In short: there's no good reason to append a newline to the (formatted representation) of the last input object. |
The purpose of |
|
Yes,
Each formatted output individually may or may not have trailing newlines (e.g., Newlines are only needed as separators between multiple formatted representations, not as a terminators. Again - there's no justification for |
|
As I said in #13071 (comment):
|
|
@ExE-Boss: When you use For in-memory processing, it is reasonable for multiple formatted representations to be joined with a separator (a newline), and not for each representation to have a terminator, which is a concept that applies to (line-oriented text) files, especially on Unix. If |
|
Isn't the crux of this that in interactive output in the terminal shows a trailing new line and when run in a script (where there often isn't an interactive session) it shouldn't? If so then that can be checked within the cmdlet whether or not the session is in non-interactive mode or not & I don't believe would be a breaking change |
|
@kilasuit, this is primarily about the trailing newline being unhelpful in programmatic processing of As for the display side of things: Both default host output and file-creation cmdlets ( Given that
Of course, you can avoid the empty array element / the need for these workarounds by stripping the trailing newline manually first - ( |
|
|
|
I think maybe mklement0 chose But consider even just a plain string! 'asdf'versus 'asdf' | Out-StringHow would one could consider that Adding newlines in is ONLY interesting where there are multiple input objects: newlines in that case are used to separate them (unless belayed by Consider also this pathological scenario: 'asdf' | Out-String | Out-String | Out-String | Out-String | Out-String Let's stop the madness and pull this burr out from under the saddle. (edit: I called that last scenario "pathological", because nobody is going to pipe through a long chain of |
|
|
|
@SteveL-MSFT, as previously stated, To provide another example: # E.g., on macOS
PS> ls / | Out-String -NoNewline
ApplicationsLibrarySystemUsersVolumesbincoresdevetchomeoptprivatesbintmpusrvar |
|
Any updates on this @SteveL-MSFT @mklement0? I'm using Out-String to generate an .xml (.nfo) file that will be picked up by Kodi to show movie details that I've scraped from an online video archive. Building up the .xml I use:
.. then insert the scraped title. What I want to end up with in the .xml file is: but what I actually end up with is something like this, which embeds newlines into the title/plot text and looks scruffy when presented in the media centre. As others have said, a |
|
@BobbyTables10, the What you're showing is an |
|
Upvote on -NoTrailingNewLine / -NoLastNewLine |
|
Is “ As demonstration, “ |
|
This problem appears to have been sneakily bothersome since |
Out-String does special handling of some PSObjects to ensure the output looks more-or-less alike to what it would've been without Out-String. Get-Date is actually a great example of this, since [Console]::Write will change 'Monday, May 23, 2022 12:02:54 PM' to '2022-05-23 12:02:50 PM', while using Out-String makes it remain the same, but with an extra trailing newline. Having a parameter like |
Also, I just noticed today, these methods write to host rather than stdout. |
Summary of the new feature/enhancement
It should be possible to use
Out-Stringin a manner that doesn't append a trailing newline, which is what it currently does and has always done.Note that
-NoNewLineis not a solution, because it suppresses all newlines, so that two or more output lines are then simply directly concatenated (like-join ''; e.g.1, 2 | Out-String -NoNewLineyields12).Here's a demonstration of the problem:
I see two options:
(a) Consider a breaking change - assuming it is justified to consider it a Bucket 3: Unlikely Grey Area change - and make
Out-Stringnever append a trailing newline.(b) Introduce a new switch,
-NoTrailingNewLine(related: #5108)The text was updated successfully, but these errors were encountered: