Running Win32 program from Bash for Windows 10

TL;DR: Microsoft didn't initially make it possible to run Windows Apps.

As of Insider Preview build 14951, announced 19 October 2016, you can run native Windows programs from Bash under Ubuntu under Windows (WSL)

Windows binaries can now be invoked directly from the WSL command line. This gives users the ability to interact with their Windows environment and system in a way that has not been possible. As a quick example, it is now possible for users to run the following commands:

$ export PATH=$PATH:/mnt/c/Windows/System32 $ notepad.exe $ ipconfig.exe | grep IPv4 | cut -d: -f2 $ ls -la | findstr.exe foo.txt $ cmd.exe /c dir

https://msdn.microsoft.com/en-us/commandline/wsl/release_notes#build-14951

The announcement said

Windows Subsystem for Linux: Today we are happy to announce two large updates to WSL!

  • Official Ubuntu 16.04 support. Ubuntu 16.04 (Xenial) is installed for all new Bash on Ubuntu on Windows instances starting in build 14951. This replaces Ubuntu 14.04 (Trusty). Existing user instances will not be upgraded automatically. Users on the Windows Insider program can upgrade manually from 14.04 to 16.04 using the do-release-upgrade command.

  • Windows / WSL interoperability. Users can now launch Windows binaries directly from a WSL command prompt. This is the number one request from our users on the WSL User Voice page.

(my emphasis)


Cause of error

The message

cannot execute binary file: Exec format error

This means Bash is convinced that adb.exe is the wrong type of file for execution.

To find out why, try using:

file /mnt/c/Android/sdk/platform-tools/adb.exe

For example:

$ pwd
/mnt/c/Windows/System32
$ ./notepad.exe
bash: ./notepad.exe: cannot execute binary file: Exec format error
$ file notepad.exe
notepad.exe: PE32+ executable (GUI) x86-64, for MS Windows

Windows Susbsystem for Linux

Note that Microsoft's initial objectives for WSL were

Using Bash, you can run command-line Linux tools and apps.

Note that does not include

  • graphical (X11) Linux tools and apps.
  • Windows tools and apps.

32-bit binaries

I believe Microsoft's Windows Subsystem for Linux (WSL) currently only supports 64-bit ELF binaries, not 32-bit ELF binaries.


Windows binaries

Also, from this question it seems WSL at time of writing this answer did not support running Windows executables from within Bash.

This is confirmed by HowtoGeek

Unfortunately, there’s no way to actually launch a Windows program or run a Windows command from within a Bash script or the Bash shell. However, you can incorporate Bash commands into a Batch script or PowerShell script


Why not :-(

If you look at how Microsoft did this you can see that Bash.exe is a native Windows console-mode app that, I believe, basically acts as a very thin front end that communicates with an unmodified /bin/bash which does all the actual interpretation and execution of commands.

WSL ... is primarily comprised of:

  • User mode session manager service that handles the Linux instance life cycle
  • Pico provider drivers (lxss.sys, lxcore.sys) that emulate a Linux kernel by translating Linux syscalls
  • Pico processes that host the unmodified user mode Linux (e.g. /bin/bash)

enter image description here
- WSL Overview

Since an unmodified /bin/bash does not expect to be able to run programs that are not native Linux "ELF" binaries, it objects to Windows "PE" binaries.

I certainly don't want to run down the rabbit hole of running WINE under WSL. I suggest you avoid this idea too.


Possible solution

See project cbwin

Launch Windows programs from "Bash on Ubuntu on Windows" (WSL)

main features:

  • Win32 command line tools in the console, invoked from WSL
  • Win32 command line tools with redirections to WSL (stdin/stdout/stderr to or from pipe/file)
  • suspend/resume propagation (Ctrl-Z suspends the Win32 processes, fg resumes them)
  • exit codes propagation
  • launch "detached" GUI Windows programs (uses start of cmd)