Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute Batch file in Windows Subsystem for Linux

Tags:

Is it possible to execute a .bat file from a Windows Subsystem for Linux (eg. Ubuntu)?

It is trivial to run an .exe file from a WSL, but I haven't found a trivial way to run a .bat. The only way I found is to open cmd.exe, but that is a bit cumbersome to do every time I need to execute a .bat file.

like image 202
oscfri Avatar asked Feb 02 '18 12:02

oscfri


People also ask

Can Windows batch file run on Linux?

No. The bat files are windows shell scripts, which probably execute windows commands and expect to run in a windows environment. You need to convert them to shell scripts in order to run them on linux, as your bash shell can not understand dos commands.

How do I run a bat file in Ubuntu?

It's pretty easy to run a batch file on Windows. Just create a file, change the extension to . bat, and either call the script in PowerShell or double click to execute it.

How do I run a batch file in Windows Explorer?

Open Start. Search for Command Prompt, right-click the top result, and select the Run as administrator option. Type the following command to run a Windows 10 batch file and press Enter: C:\PATH\TO\FOLDER\BATCH-NAME. bat.


2 Answers

Unfortunately at the moment you cannot do so without either using:

cmd.exe /c foo.bat

…or the following hack using binfmt:

sudo sh -c "echo :WindowsBatch:E::bat::/init: > /proc/sys/fs/binfmt_misc/register"

You could then just type:

foo.bat

The problems with this method are that you'd need to be root, run it each time you opened a bash window, probably do the same for .cmd files too and, I suppose, any bash script name ending with .bat could have issues!

I guess until Microsoft deals with this issue, you're limited to the above.

like image 99
Compo Avatar answered Oct 24 '22 20:10

Compo


If the first answer doesn't work, try:

sudo sh -c "echo :WindowsBatch:E::bat::/init: > /proc/sys/fs/binfmt_misc/register"

Then, locate the file. Then make the file executable:

chmod +x foo.bat

Then run the file:

./foo.bat

If it says permission denied, just try running it with sudo

like image 23
Ash 12312 Avatar answered Oct 24 '22 19:10

Ash 12312