-
Notifications
You must be signed in to change notification settings - Fork 842
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
add ability to run Windows batch files in bash.exe #1917
Comments
Ooh, I like that trick. For |
There is But in general I'm not sure if it should be the default because an extension like Just my two cents though. |
Right now we can run Windows executables from WSL, but batch files don't do so well:
Would like to see this updated too :) |
Actually, this should probably be closed, and a new bug opened up to properly handle /etc/binfmt.d. |
After looking into this further, binfmt is already fully supported (although not via /some/dir$ sudo update-binfmts --install WindowsBat /init --extension bat
/some/dir$ sudo update-binfmts --install WindowsCmd /init --extension cmd # (if desired) ..sadly, this doesn't work with In any case, it addresses the original issue. When that is set, files with those extensions are treated as scripts for Whether that should be a default setting is up to someone else. |
It repeats the original issue. Your
|
No, it doesn't just repeat it, it also sets a configuration (somewhere) so it remembers the setting. At least, it did on my system. |
update-binfmts works for me. In Ubuntu 18.04.2 LTS, I need to first run |
If you use alias -s {cmd,bat}='cmd.exe /c' |
This was not working for script that are not in the current directory due to the difference between windows and unix paths. Because I'm working on a project that sometimes has scripts launching other bat files this was a problem. I ended up writing the following script : #!/bin/bash
exec wslpath -w "$1" | cmd.exe /c Then saved it as /usr/local/bin/wsl_to_cmd (or anywhere in the PATH) then just defined the alias as follows : alias -s {cmd,bat}='wsl_to_cmd' |
Thank you! It is |
Yes sorry, typo on my end, I updated my answer
Even better ! It does look like it works yes ! |
What's wrong with the solution I posted 3 years ago? |
If you mean that in the starting comment, then it lists downsides, such as
|
Running it everytime I launch a wsl window is not ideal. I open a lot of wsl windows. Also the update-binfmts command doesn't seem to work on my machine (although to be fair I didn't look too closely at it). Being a zsh user I thought if I could do it with an alias, it would be the easiest and least disruptive solution. Also it will carry over with my zshrc if I change computer at some point. |
Try _wslbatch() { eval cmd.exe /c "$(wslpath -w "$1")"; } &&
alias -s {cmd,bat}='_wslbatch' From what I understand about |
And who is running your 'alias' command for you?? Presumably you put it in your .bashrc and you can do the same with the binfmt command. I've been doing it this way for years. Problem solved and long forgotten... |
You're right I could put it in my .zshrc as well, but, like I said, I couldn't get the binfmt command to work on my end. The alias worked so I went with that. Both solutions are fine really. |
One problem I have with
If the batch file takes arguments, this gets even more complicated. Due to #4868, any filepaths with symlinks anywhere in them need to be resolved prior to passing them to the batch file. Also, when passing filepaths with spaces, they must be escaped for This is even uglier, and at the moment I can't actually get it to work. I think giving WSL the ability to run |
My solution above is very similar to one given here, but that one doesn't handle the issues with file-path arguments. |
This is still unresolved. |
Currently you can run a batch file using "cmd.exe /c foo.bat" but that is awkward, difficult to type, and possibly inefficient. A better way is to use binfmt like we do for PE files:
sudo sh -c "echo :WindowsBatch:E::bat::/init: > /proc/sys/fs/binfmt_misc/register"
Using this technique you can run the batch file using just "foo.bat". However, the command above needs to be run every time you open a bash window. That is inconvenient especially because it requires root. You can obviate the password check using /etc/sudoers and put the command in your .bashrc but that is not ideal.
Is this something we should consider making part of the default installation? The only downside I can think of is that it won't let you run a bash script that ends in ".bat" but that is an unlikely scenario and in that case the user can rename the file using a more suitable extension such as ".sh".
The text was updated successfully, but these errors were encountered: