0

My .bat file contains

wsl.exe && cd /mnt/d/ && ls
pause

The .bat file opens Windows Subsystem Linux (Ubuntu 20.04.4, WSL 2), execute wsl.exe command, and then... nothing happens.

Strangely enough, when I copy-paste the contents of the .bat file into CMD, everything works nicely.

Any help is welcome.

abc
  • 147
  • 1
  • 3
  • 17
  • 1
    The first duplicate linked above has nothing to do with this question (and the one answer that covers the "vice-versa" is quite out-of-date, so beware), but the [second one](https://stackoverflow.com/questions/70379693/how-to-create-a-windows-batch-script-that-opens-both-wsl-ubuntu-and-firefox) (now closed) does have an answer (by me) that will hopefully help. I think I've done a few others with more information about the "general case" that I'll link below when I find them. – NotTheDr01ds May 04 '22 at 11:50
  • 1
    Look at the first example I provide in [this answer](https://stackoverflow.com/a/69533520/11810933). That should be what you want to use for your `wsl` command in this particular example -- `wsl -e sh -c "cd /mnt/d && ls"`. – NotTheDr01ds May 04 '22 at 11:54
  • Thanks a lot! How to insert comments for the commands? – abc May 07 '22 at 20:41
  • 1
    Typically, if you are passing in enough commands that you need to document them with comments, you would be better off creating a script *inside* WSL and then calling it (from your bat file) with `wsl -e /path/to/script`, putting the comments in the shell script. In PowerShell, it's also possible to create multiline strings for the `-c`, and comments could be added there, but not for CMD/bat files. – NotTheDr01ds May 07 '22 at 23:35

0 Answers0