Skip to content
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

NewLine is added to output when told to explicitly drop NewLine #15222

Closed
johnjelinek opened this issue Apr 13, 2021 · 18 comments
Closed

NewLine is added to output when told to explicitly drop NewLine #15222

johnjelinek opened this issue Apr 13, 2021 · 18 comments
Labels
Needs-Triage The issue is new and needs to be triaged by a work group. Resolution-No Activity Issue has had no activity for 6 months or more WG-Cmdlets-Utility cmdlets in the Microsoft.PowerShell.Utility module

Comments

@johnjelinek
Copy link

A newline is getting added to the output when running echo even if I tell it not to (with -n flag). It also looks like Write-Out doesn't support -NoNewLine.

Steps to reproduce

echo 'hello' | base64

Expected behavior

> echo 'hello' | base64
aGVsbG8K
> echo -n 'hello' | base64
aGVsbG8=

Actual behavior

> echo 'hello' | base64
aGVsbG8K
> echo -n 'hello' | base64
aGVsbG8K

Environment data

> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.1.3
PSEdition                      Core
GitCommitId                    7.1.3
OS                             Darwin 19.6.0 Darwin Kernel Version 19.6.0: Tue Jan 12 22:13:05 PST 2021; root:xnu-6153.141.16~1/RELEASE_X86_64
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0
@johnjelinek johnjelinek added the Needs-Triage The issue is new and needs to be triaged by a work group. label Apr 13, 2021
@vexx32
Copy link
Collaborator

vexx32 commented Apr 13, 2021

echo is an alias for Write-Output. Your -n is treated as -NoEnumerate, not -NoNewLine (as you said, Write-Output doesn't have a -NoNewLine).

Pretty sure this is a bit of a wider issue, given that simply "hello" | base64 exhibits the same issue.

@mklement0
Copy link
Contributor

Yes, unfortunately the trailing newline can currently not be prevented - please see #5974

@jborean93
Copy link
Collaborator

jborean93 commented Apr 13, 2021

As a workaround you can use the .NET method instead of relying on platform specific binaries being place.

[Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes('hello'))

It's more verbose but the benefit is that it it's portable, unlike calling base64 which won't work on hosts without that binary. You also have fine control over the encoding used on the string.

But if base64 is just an example to show your problem and the issue is around piping data has the newline then mkelement0's issue #5974 covers that.

@iSazonov iSazonov added the WG-Cmdlets-Utility cmdlets in the Microsoft.PowerShell.Utility module label Apr 14, 2021
@mklement0

This comment has been minimized.

@SeeminglyScience

This comment has been minimized.

@jborean93

This comment has been minimized.

@mklement0

This comment has been minimized.

@johnjelinek
Copy link
Author

@jborean93: so, I'm the only one on the team using pwsh for mac right now. The rest are on zsh/fish -- I wanted to write docs that are portable to the rest of my team. The .NET way won't work for my use-case. 'Tis a shame, I will have to switch shells to be pragmatic for now.

@mklement0
Copy link
Contributor

mklement0 commented Apr 14, 2021

@johnjelinek, calling the platform-native shell is always an option:

PS> sh -c "printf %s 'hello' | base64"
aGVsbG8K=

@mklement0
Copy link
Contributor

mklement0 commented Apr 14, 2021

To generalize the approach (see caveat below):

PS> sh -c 'printf %s "$1" | base64' - 'hello'
aGVsbG8K=

@mklement0
Copy link
Contributor

mklement0 commented Apr 14, 2021

P.S., @johnjelinek: passing a string such as 'printf %s "$1" | base64' to external programs (such as sh) currently results in the embedded " characters effectively getting stripped, which is a longstanding problem - see #1995 (comment). PR #14692 is aiming to fix it on an opt-in basis, initially as an experimental feature. #15143 additionally proposes important accommodations for command-line programs on Windows.

@johnjelinek
Copy link
Author

Ok, thanks for the help! Do we want to leave this open or close the issue?

@mklement0
Copy link
Contributor

@johnjelinek:

  • Your original (legitimate) complaint is covered by Piping Text To An External Program Appends A Trailing Newline #5974, so, yes, I suggest closing this issue.

  • Independently, if something is unclear about the comments above - as your use of the 😕 emoji suggests - feel free to ask for clarification.

    • By contrast, if your use of 😕 is meant to express disapproval of the status quo, I suggest expressing that verbally, to avoid confusion.

@mklement0
Copy link
Contributor

PowerShell Core 7.2.0-preview.5 has just been released, which includes the PSNativeCommandArgumentPassing experimental feature that fixes the embedded-double-quotes problem fully on Unix, and partially on Windows. (Preview releases have experimental features turned on by default, but not release candidates and regular releases).

Thus, in PowerShell Core 7.2.0-preview.5 the following now works correctly on Unix-like platforms (assuming the experimental feature is on and the $PSNativeCommandArgumentPassing preference variable is at its default, 'Standard'):

# Malfunctions in PowerShell Core 7.2.0-preview.4 and below, because `"$1"` turns into `$1`, which
# makes `sh` perform word-splitting, resulting in *two* arguments.
PS> sh -c 'printf %s "$1" | base64' - 'hello more'
aGVsbG8gbW9yZQ==

Copy link
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.

2 similar comments
Copy link
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.

Copy link
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 bot added Resolution-No Activity Issue has had no activity for 6 months or more labels Nov 16, 2023
Copy link
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
Labels
Needs-Triage The issue is new and needs to be triaged by a work group. Resolution-No Activity Issue has had no activity for 6 months or more WG-Cmdlets-Utility cmdlets in the Microsoft.PowerShell.Utility module
Projects
None yet
Development

No branches or pull requests

6 participants