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

>> operator in WSL redirects to Windows path #5551

Closed
bkotch opened this issue Jul 6, 2020 · 2 comments
Closed

>> operator in WSL redirects to Windows path #5551

bkotch opened this issue Jul 6, 2020 · 2 comments

Comments

@bkotch
Copy link

bkotch commented Jul 6, 2020

Environment

[Environment]::OSVersion

Platform ServicePack Version      VersionString
-------- ----------- -------      -------------
 Win32NT             10.0.17763.0 Microsoft Windows NT 10.0.17763.0

wsl lsb_release -r

Release:        18.04

wsl cat /proc/version

Linux version 4.4.0-17763-Microsoft ([email protected]) (gcc version 5.4.0 (GCC) ) #1217-Microsoft Mon May 05 16:09:00 PST 2020

Steps to reproduce

Run a command that pipes the output to another linux command inside Powershell
e.g.

wsl echo "test" >> ~/test.txt 
ls  ~/test.txt

ls (correctly) reads ~ as the Windows home directory. That isn't the problem. The problem is that the >> wrote to the windows directory and not the wsl user's home directory

The only way I could get this to work is by escaping the >> operators so they get interpreted by the WSL *nix appliance like so this writes the file where'd I expect.

wsl echo "test" `>`> ~/text.txt
wsl ls  ~/test.txt

I found the issue with absolute paths as well

PS C:\Users\bhatchet> wsl echo "test" >> /home/root/test.txt
out-file : Could not find a part of the path 'C:\home\root\test.txt'. 
At line:1 char:1
+ wsl echo "test" >> /home/root/test.txt
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OpenError: (:) [Out-File], DirectoryNotFoundException 
    + FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.OutFileCommand

I get that powershell is escaping the >> commands so ~ evaluates to the windows home directory of the user and escaping works, but it gets pretty hairy pretty quickly.

Like, creating a linux user from outside wsl

wsl useradd -p `$`("openssl passwd -1 $($password)"`) $username

Expected behavior

Piping / redirecting *nix commands stay relative to *nix inside wsl execute

Actual behavior

Piping / redirecting *nix commands use windows after the redirect inside wsl execute.

@therealkenc
Copy link
Collaborator

therealkenc commented Jul 6, 2020

This is variation #5232 (message) #5270 #3284. That ~ is not interpreted by bash (your Linux shell). For your first scenario you want:

PS> wsl.exe sh -c "echo test > ~/test.txt"

@sundhaug92
Copy link

Amend to what @therealkenc said: That ls is an alias of Get-ChildItem, it's the powershell-version (You can verify this with for example ls ~/desktop (verifying via case-insensitivity and desktop-folder existence)

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

No branches or pull requests

3 participants