How To Invoke Any Windows Executable Inside WSL

0

Windows 10’s Windows Subsystem for Linux (WSL) is like a magic show. But like any magic, behind the curtain lies the ultimate truth. If you’d like to learn more about how everything worked in Windows 10 in regards to how it executed your command, be sure to check out the blog post below from Microsoft. In it, they explained what happened when a  command received from the Bash shell to execute the command.

Windows Subsystem for Linux Overview

In this post, we will show you some of the examples on invoking a Windows executable inside a Bash shell that’s running Windows Subsystem for Linux. That is, start a Windows program inside a Linux environment, how cool is that?

Example 1: Launch cmd.exe inside Bash

Start by launch Bash on Ubuntu on Windows, or another Linux distribution available on Windows Store. We will be using Ubuntu’s bash, type

cmd.exe

To launch Windows’ command prompt. Note it has to be “cmd.exe” exact, you can’t type “cmd” nor other Windows executable like “Explorer.exe”. There is a special hook in bash that would trigger command line prompt when entered “cmd.exe”

cmd in WSL - How To Invoke Any Windows Executable Inside WSL

Here I tried to launch “ver” to print out the Windows version inside cmd in Bash.

Example 2: Launch Chrome via Bash

Now let’s try a real example, say we want to launch Chrome inside Linux. Here is how to do it.

First I created an alias command and append that to “.bashrc” for easier access when need to trigger this command.

alias launchchrome="\"/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe\""

launch chrome in WSL 1 - How To Invoke Any Windows Executable Inside WSL

When we want to launch Chrome, just enter “launchchrome” or whatever the shorthand name you have assigned to this command. Note that you have to escape properly like above in order for the alias command to work. Likewise, for all other Windows native executables installed on your Windows partition, you can reference with a bash shell script in WSL.

You might ask what’s the big deal, the regular double-click or launch via cmd.exe does exactly the same trick. True, they all achieve the same end result, however, the fact that you can reference Windows executable inside a Linux shell script opens up a whole new world of automation. For developers that are coming from Linux background, they don’t have to use PowerShell to run a complicated script. And for new developers, they get access to great build chain tool that is native to Linux without using a Linux desktop GUI environment. The possibility is endless, give them a try!

LEAVE A REPLY

Please enter your comment!
Please enter your name here