3

wsl -h shows the following:

    --exec, -e <CommandLine>   Execute the specified command without using the default Linux shell.
    --   Pass the remaining command line as is.

What does "without using the default Linux shell" mean (i.e. what else is it going to use, if not the default shell!?)?.

Additionally, by way of an example, I now have three possible ways to run Linux ls from my PowerShell prompt (i.e. this will not be Get-ChildItem aliased to ls, but instead a Linux command via WSL):

PS C:\> wsl -e ls   # Alternatively,  wsl --exec ls
PS C:\> wsl -- ls
PS C:\> wsl ls

But all outputs appear to be the same. How would you explain the differences between these three ways of running a WSL Linux command from a PowerShell prompt?

YorSubs
  • 3,194
  • 7
  • 37
  • 60
  • 2
    with wsl ... /bin/bash is used. With wsl -e .... The shell is env. This has more relevance when you are running shell scripts on wsl. – Raman Sailopal Feb 11 '21 at 09:12

2 Answers2

4

I think it means wsl runs the command directly, instead of spawning a shell process to run the command.

For example, if I run :

wsl -e sleep 10

From another terminal, I have :

root      1482     1  0 11:32 tty3     00:00:00 /init
ubuntu    1483  1482  0 11:32 tty3     00:00:00 sleep 10

We can see /init is the parent of sleep 10, without a shell in between.

Philippe
  • 20,025
  • 2
  • 23
  • 32
0

A cool trick is using this to set the X11 $DISPLAY variable, letting you use windows terminal to get remote shells using WSLG.

# in microsoft terminal or powershell use this command line
wsl.exe -- ssh -a -X -Y $hostname

then on the remote system

# DISPLAY will show something like localhost:10.0 on the remote system
echo $DISPLAY
# use a program like xeyes to test
xeyes
Brian Horakh
  • 329
  • 4
  • 12
  • This sounds good, but I tried it and can't get it to work. Where you say `$hostname` I assumed you meant replacing that by the IP or hostname of the remote Linux system, I've tried both ways, and all that happens is that I ssh onto the remote system. Then, `echo $DISPLAY` shows nothing ... Typing `xeyes` results in `Error: Can't open display:`. Are you running on Windows 11? I'm using Windows 10, but I have setup my WSL to support GUI apps, so I thought this should work. – YorSubs May 03 '22 at 08:13
  • @YorSubs yes, you need to have Win11, or at least a recently patched windows 10. WSLG is in windows 11. You need to have WSL2 + WSLG specifically. https://github.com/microsoft/wslg/issues/730 – Brian Horakh May 05 '22 at 08:31
  • https://github.com/microsoft/terminal/issues/13041 – Brian Horakh May 05 '22 at 08:32