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

Quoting pipe commands with wsl.exe #3284

Closed
pe224 opened this issue Jun 8, 2018 · 6 comments
Closed

Quoting pipe commands with wsl.exe #3284

pe224 opened this issue Jun 8, 2018 · 6 comments
Labels

Comments

@pe224
Copy link

pe224 commented Jun 8, 2018

Windows build number: 10.0.17134.81

I'm not quite sure how to feed linux commands using pipes into wsl.exe

Example:
wsl.exe echo test | wc
wsl.exe "echo test | wc"
wsl.exe 'echo test | wc'
all fail, whereas
ubuntu.exe run "echo test | wc"
produces the desired output 1 1 5.

There is a comment in this issue trying to clear it up.
It claims that wsl.exe <cmd> internally calls ["/bin/bash", "-c", <cmd>],
but wsl /bin/bash -c "echo test | wc" produces correct output whereas the commands above do not.

I also tried escaping the double quotes without success.

@0xbadfca11
Copy link

wsl.exe echo test ^| wc

@therealkenc
Copy link
Collaborator

wsl.exe echo test ^| wc

+1 clever

You can also do:

wsl.exe /bin/bash -c "echo test | wc"

@utkarshagarwal01
Copy link

utkarshagarwal01 commented Sep 18, 2019

Maybe wsl.exe /bin/bash -c "echo test | wc" could be added to the documentation here:
https://docs.microsoft.com/en-us/windows/wsl/interop#run-linux-tools-from-a-windows-command-line

The document mentions

The Linux command following wsl.exe is handled like any command run in WSL. Things such as sudo, piping, and file redirection work.

which doesnt look like the case.

Also, wsl.exe echo test ^| wc doesn't work for me anymore.

wc : The term 'wc' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that
the path is correct and try again.
At line:1 char:27
wsl.exe echo Readme.md ^| wc
~~
CategoryInfo : ObjectNotFound: (wc:String) [], CommandNotFoundException
FullyQualifiedErrorId : CommandNotFoundException

@therealkenc
Thanks.

@therealkenc
Copy link
Collaborator

therealkenc commented Sep 18, 2019

The Linux command following wsl.exe is handled like any command run in WSL. Things such as sudo, piping, and file redirection work.

Can't vouch for the documentation. Looking at the page you cite they seem to mean by 'piping' in the context of cmd.exe. Complaint department for documentation improvements would be here.

wsl.exe echo Readme.md ^| wc
...for me anymore.

I can't say if that example ever worked; it might have, I literally can't say and wouldn't deign to try an old version to find out.

[edit] It appears to, in 18980, anyway.

image

Still....

Whether it did or it didn't (it did), well formed command is:

C:\> wsl.exe /bin/sh -c "cd /path/to/readme; echo ./Readme.md | wc"

Windows CLI arguments don't work like Unix CLI arguments; never have and never will. To pretend they do is a mugs game. YRMV.

@0xbadfca11
Copy link

When you using powershell, escape character is ` instead of ^.

@DGrv
Copy link

DGrv commented May 18, 2023

If you wanna save the ouput to a variable you have to again reuse ^ for the 2 characters ^| :

for /f %%i in ('wsl echo 7.8*1000/1 ^^^| bc') do set delaysound2=%%i

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants